@@ -28,155 +28,158 @@ export class Websocket {
28
28
private connect ( host : string ) : void {
29
29
this . options . debug && console . log ( 'Trying to connect...' ) ;
30
30
31
- this . websocket = new WebSocket ( host )
31
+ this . websocket = new WebSocket ( host ) ;
32
32
33
33
this . websocket . onerror = ( ) => {
34
34
if ( ! this . hasConnected ) {
35
35
setTimeout ( ( ) => {
36
- this . socketId = undefined
37
- this . connect ( host )
36
+ this . socketId = undefined ;
37
+ this . connect ( host ) ;
38
38
} , 3000 ) ;
39
39
}
40
- }
40
+ } ;
41
41
42
42
this . websocket . onopen = ( ) => {
43
43
this . options . debug && console . log ( 'Connected !' ) ;
44
44
this . hasConnected = true ;
45
+
45
46
this . send ( {
46
47
event : 'whoami' ,
47
- } )
48
+ } ) ;
48
49
49
50
while ( this . buffer . length ) {
50
51
const message = this . buffer [ 0 ]
51
52
52
53
this . send ( message )
53
54
54
55
this . buffer . splice ( 0 , 1 )
55
- }
56
+ } ;
56
57
57
58
// Register events only once connected, or they won't be registered if connection failed/lost
58
59
59
60
this . websocket . onmessage = ( messageEvent : MessageEvent ) => {
60
- const message = this . parseMessage ( messageEvent . data )
61
- this . options . debug && console . log ( 'onmessage' , messageEvent . data )
61
+ const message = this . parseMessage ( messageEvent . data ) ;
62
+ this . options . debug && console . log ( 'onmessage' , messageEvent . data ) ;
62
63
63
64
if ( ! message ) {
64
- return
65
+ return ;
65
66
}
66
67
67
68
if ( message . channel ) {
68
- this . options . debug && console . log ( `Received event ${ message . event } on channel ${ message . channel } ` )
69
+ this . options . debug && console . log ( `Received event ${ message . event } on channel ${ message . channel } ` ) ;
69
70
70
71
if ( this . listeners [ message . channel ] && this . listeners [ message . channel ] [ message . event ] ) {
71
- this . listeners [ message . channel ] [ message . event ] ( message . data )
72
+ this . listeners [ message . channel ] [ message . event ] ( message . data ) ;
72
73
}
73
74
74
- return
75
+ return ;
75
76
}
76
77
77
78
if ( this . internalListeners [ message . event ] ) {
78
- this . internalListeners [ message . event ] ( message . data )
79
+ this . internalListeners [ message . event ] ( message . data ) ;
79
80
}
80
81
}
81
82
82
83
83
84
// send ping every 60 seconds to keep connection alive
84
85
this . pingInterval = setInterval ( ( ) => {
85
86
if ( this . websocket . readyState === this . websocket . OPEN ) {
86
- this . options . debug && console . log ( 'Sending ping' )
87
+ this . options . debug && console . log ( 'Sending ping' ) ;
88
+
87
89
this . send ( {
88
90
event : 'ping' ,
89
- } )
91
+ } ) ;
90
92
}
91
- } , 60 * 1000 )
93
+ } , 60 * 1000 ) ;
92
94
}
93
95
94
96
95
97
this . websocket . onclose = ( ) => {
96
98
this . options . debug && console . info ( 'Connection closed.' ) ;
99
+
97
100
if ( this . closing ) {
98
101
return ;
99
102
}
100
- this . hasConnected = false
103
+
104
+ this . hasConnected = false ;
101
105
this . options . debug && console . info ( 'Connection lost, reconnecting...' ) ;
106
+
102
107
setTimeout ( ( ) => {
103
- this . socketId = undefined
104
- this . connect ( host )
108
+ this . socketId = undefined ;
109
+ this . connect ( host ) ;
105
110
} , 1000 ) ;
106
111
} ;
107
112
108
113
this . on ( 'whoami' , ( { socket_id : socketId } ) => {
109
- this . socketId = socketId
114
+ this . socketId = socketId ;
110
115
111
- this . options . debug && console . log ( `just set socketId to ${ socketId } ` )
116
+ this . options . debug && console . log ( `just set socketId to ${ socketId } ` ) ;
112
117
113
118
// Handle the backlog and don't empty it, we'll need it if we lose connection
114
119
let channel : Channel ;
120
+
115
121
for ( channel of this . channelBacklog ) {
116
- this . actuallySubscribe ( channel )
122
+ this . actuallySubscribe ( channel ) ;
117
123
}
118
-
119
- } )
120
-
121
-
124
+ } ) ;
122
125
}
123
126
124
127
constructor ( options : Options ) {
125
128
this . options = options ;
126
129
127
130
this . connect ( this . options . host ) ;
128
131
129
- return this
132
+ return this ;
130
133
}
131
134
132
135
protected parseMessage ( body : string ) : MessageBody {
133
136
try {
134
- return JSON . parse ( body )
137
+ return JSON . parse ( body ) ;
135
138
} catch ( error ) {
136
- this . options . debug && console . error ( error )
139
+ this . options . debug && console . error ( error ) ;
137
140
138
- return undefined
141
+ return undefined ;
139
142
}
140
143
}
141
144
142
145
getSocketId ( ) : string {
143
- return this . socketId
146
+ return this . socketId ;
144
147
}
145
148
146
149
private socketIsReady ( ) : boolean {
147
- return this . websocket . readyState === this . websocket . OPEN
150
+ return this . websocket . readyState === this . websocket . OPEN ;
148
151
}
149
152
150
153
send ( message : object ) : void {
151
154
if ( this . socketIsReady ( ) ) {
152
- this . websocket . send ( JSON . stringify ( message ) )
153
- return
155
+ this . websocket . send ( JSON . stringify ( message ) ) ;
156
+ return ;
154
157
}
155
158
156
- this . buffer . push ( message )
159
+ this . buffer . push ( message ) ;
157
160
}
158
161
159
162
close ( ) : void {
160
- this . closing = true
161
- this . internalListeners = { }
163
+ this . closing = true ;
164
+ this . internalListeners = { } ;
162
165
163
- clearInterval ( this . pingInterval )
164
- this . pingInterval = undefined
166
+ clearInterval ( this . pingInterval ) ;
167
+ this . pingInterval = undefined ;
165
168
166
- this . websocket . close ( )
169
+ this . websocket . close ( ) ;
167
170
}
168
171
169
172
subscribe ( channel : Channel ) : void {
170
173
if ( this . getSocketId ( ) ) {
171
- this . actuallySubscribe ( channel )
174
+ this . actuallySubscribe ( channel ) ;
172
175
} else {
173
- this . channelBacklog . push ( channel )
176
+ this . channelBacklog . push ( channel ) ;
174
177
}
175
178
}
176
179
177
180
private actuallySubscribe ( channel : Channel ) : void {
178
181
if ( channel . name . startsWith ( 'private-' ) || channel . name . startsWith ( 'presence-' ) ) {
179
- this . options . debug && console . log ( `Sending auth request for channel ${ channel . name } ` )
182
+ this . options . debug && console . log ( `Sending auth request for channel ${ channel . name } ` ) ;
180
183
181
184
if ( this . options . bearerToken ) {
182
185
this . options . auth . headers [ 'Authorization' ] = 'Bearer ' + this . options . bearerToken ;
@@ -188,28 +191,28 @@ export class Websocket {
188
191
} , {
189
192
headers : this . options . auth . headers || { }
190
193
} ) . then ( ( response : AxiosResponse ) => {
191
- this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` )
194
+ this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` ) ;
192
195
193
196
this . send ( {
194
197
event : 'subscribe' ,
195
198
data : {
196
199
channel : channel . name ,
197
200
...response . data
198
201
} ,
199
- } )
202
+ } ) ;
200
203
} ) . catch ( ( error ) => {
201
- this . options . debug && console . log ( `Auth request for channel ${ channel . name } failed` )
202
- this . options . debug && console . error ( error )
204
+ this . options . debug && console . log ( `Auth request for channel ${ channel . name } failed` ) ;
205
+ this . options . debug && console . error ( error ) ;
203
206
} )
204
207
} else {
205
- this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` )
208
+ this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` ) ;
206
209
207
210
this . send ( {
208
211
event : 'subscribe' ,
209
212
data : {
210
213
channel : channel . name ,
211
214
} ,
212
- } )
215
+ } ) ;
213
216
}
214
217
}
215
218
@@ -219,28 +222,28 @@ export class Websocket {
219
222
data : {
220
223
channel : channel . name ,
221
224
} ,
222
- } )
225
+ } ) ;
223
226
224
227
if ( this . listeners [ channel . name ] ) {
225
- delete this . listeners [ channel . name ]
228
+ delete this . listeners [ channel . name ] ;
226
229
}
227
230
}
228
231
229
232
on ( event : string , callback : Function = null ) : void {
230
- this . internalListeners [ event ] = callback
233
+ this . internalListeners [ event ] = callback ;
231
234
}
232
235
233
236
bind ( channel : Channel , event : string , callback : Function ) : void {
234
237
if ( ! this . listeners [ channel . name ] ) {
235
- this . listeners [ channel . name ] = { }
238
+ this . listeners [ channel . name ] = { } ;
236
239
}
237
240
238
- this . listeners [ channel . name ] [ event ] = callback
241
+ this . listeners [ channel . name ] [ event ] = callback ;
239
242
}
240
243
241
244
unbindEvent ( channel : Channel , event : string , callback : Function = null ) : void {
242
245
if ( this . internalListeners [ event ] && ( callback === null || this . internalListeners [ event ] === callback ) ) {
243
- delete this . internalListeners [ event ]
246
+ delete this . internalListeners [ event ] ;
244
247
}
245
248
}
246
249
}
0 commit comments