@@ -28,155 +28,158 @@ export class Websocket {
2828 private connect ( host : string ) : void {
2929 this . options . debug && console . log ( 'Trying to connect...' ) ;
3030
31- this . websocket = new WebSocket ( host )
31+ this . websocket = new WebSocket ( host ) ;
3232
3333 this . websocket . onerror = ( ) => {
3434 if ( ! this . hasConnected ) {
3535 setTimeout ( ( ) => {
36- this . socketId = undefined
37- this . connect ( host )
36+ this . socketId = undefined ;
37+ this . connect ( host ) ;
3838 } , 3000 ) ;
3939 }
40- }
40+ } ;
4141
4242 this . websocket . onopen = ( ) => {
4343 this . options . debug && console . log ( 'Connected !' ) ;
4444 this . hasConnected = true ;
45+
4546 this . send ( {
4647 event : 'whoami' ,
47- } )
48+ } ) ;
4849
4950 while ( this . buffer . length ) {
5051 const message = this . buffer [ 0 ]
5152
5253 this . send ( message )
5354
5455 this . buffer . splice ( 0 , 1 )
55- }
56+ } ;
5657
5758 // Register events only once connected, or they won't be registered if connection failed/lost
5859
5960 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 ) ;
6263
6364 if ( ! message ) {
64- return
65+ return ;
6566 }
6667
6768 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 } ` ) ;
6970
7071 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 ) ;
7273 }
7374
74- return
75+ return ;
7576 }
7677
7778 if ( this . internalListeners [ message . event ] ) {
78- this . internalListeners [ message . event ] ( message . data )
79+ this . internalListeners [ message . event ] ( message . data ) ;
7980 }
8081 }
8182
8283
8384 // send ping every 60 seconds to keep connection alive
8485 this . pingInterval = setInterval ( ( ) => {
8586 if ( this . websocket . readyState === this . websocket . OPEN ) {
86- this . options . debug && console . log ( 'Sending ping' )
87+ this . options . debug && console . log ( 'Sending ping' ) ;
88+
8789 this . send ( {
8890 event : 'ping' ,
89- } )
91+ } ) ;
9092 }
91- } , 60 * 1000 )
93+ } , 60 * 1000 ) ;
9294 }
9395
9496
9597 this . websocket . onclose = ( ) => {
9698 this . options . debug && console . info ( 'Connection closed.' ) ;
99+
97100 if ( this . closing ) {
98101 return ;
99102 }
100- this . hasConnected = false
103+
104+ this . hasConnected = false ;
101105 this . options . debug && console . info ( 'Connection lost, reconnecting...' ) ;
106+
102107 setTimeout ( ( ) => {
103- this . socketId = undefined
104- this . connect ( host )
108+ this . socketId = undefined ;
109+ this . connect ( host ) ;
105110 } , 1000 ) ;
106111 } ;
107112
108113 this . on ( 'whoami' , ( { socket_id : socketId } ) => {
109- this . socketId = socketId
114+ this . socketId = socketId ;
110115
111- this . options . debug && console . log ( `just set socketId to ${ socketId } ` )
116+ this . options . debug && console . log ( `just set socketId to ${ socketId } ` ) ;
112117
113118 // Handle the backlog and don't empty it, we'll need it if we lose connection
114119 let channel : Channel ;
120+
115121 for ( channel of this . channelBacklog ) {
116- this . actuallySubscribe ( channel )
122+ this . actuallySubscribe ( channel ) ;
117123 }
118-
119- } )
120-
121-
124+ } ) ;
122125 }
123126
124127 constructor ( options : Options ) {
125128 this . options = options ;
126129
127130 this . connect ( this . options . host ) ;
128131
129- return this
132+ return this ;
130133 }
131134
132135 protected parseMessage ( body : string ) : MessageBody {
133136 try {
134- return JSON . parse ( body )
137+ return JSON . parse ( body ) ;
135138 } catch ( error ) {
136- this . options . debug && console . error ( error )
139+ this . options . debug && console . error ( error ) ;
137140
138- return undefined
141+ return undefined ;
139142 }
140143 }
141144
142145 getSocketId ( ) : string {
143- return this . socketId
146+ return this . socketId ;
144147 }
145148
146149 private socketIsReady ( ) : boolean {
147- return this . websocket . readyState === this . websocket . OPEN
150+ return this . websocket . readyState === this . websocket . OPEN ;
148151 }
149152
150153 send ( message : object ) : void {
151154 if ( this . socketIsReady ( ) ) {
152- this . websocket . send ( JSON . stringify ( message ) )
153- return
155+ this . websocket . send ( JSON . stringify ( message ) ) ;
156+ return ;
154157 }
155158
156- this . buffer . push ( message )
159+ this . buffer . push ( message ) ;
157160 }
158161
159162 close ( ) : void {
160- this . closing = true
161- this . internalListeners = { }
163+ this . closing = true ;
164+ this . internalListeners = { } ;
162165
163- clearInterval ( this . pingInterval )
164- this . pingInterval = undefined
166+ clearInterval ( this . pingInterval ) ;
167+ this . pingInterval = undefined ;
165168
166- this . websocket . close ( )
169+ this . websocket . close ( ) ;
167170 }
168171
169172 subscribe ( channel : Channel ) : void {
170173 if ( this . getSocketId ( ) ) {
171- this . actuallySubscribe ( channel )
174+ this . actuallySubscribe ( channel ) ;
172175 } else {
173- this . channelBacklog . push ( channel )
176+ this . channelBacklog . push ( channel ) ;
174177 }
175178 }
176179
177180 private actuallySubscribe ( channel : Channel ) : void {
178181 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 } ` ) ;
180183
181184 if ( this . options . bearerToken ) {
182185 this . options . auth . headers [ 'Authorization' ] = 'Bearer ' + this . options . bearerToken ;
@@ -188,28 +191,28 @@ export class Websocket {
188191 } , {
189192 headers : this . options . auth . headers || { }
190193 } ) . 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 } ` ) ;
192195
193196 this . send ( {
194197 event : 'subscribe' ,
195198 data : {
196199 channel : channel . name ,
197200 ...response . data
198201 } ,
199- } )
202+ } ) ;
200203 } ) . 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 ) ;
203206 } )
204207 } else {
205- this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` )
208+ this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` ) ;
206209
207210 this . send ( {
208211 event : 'subscribe' ,
209212 data : {
210213 channel : channel . name ,
211214 } ,
212- } )
215+ } ) ;
213216 }
214217 }
215218
@@ -219,28 +222,28 @@ export class Websocket {
219222 data : {
220223 channel : channel . name ,
221224 } ,
222- } )
225+ } ) ;
223226
224227 if ( this . listeners [ channel . name ] ) {
225- delete this . listeners [ channel . name ]
228+ delete this . listeners [ channel . name ] ;
226229 }
227230 }
228231
229232 on ( event : string , callback : Function = null ) : void {
230- this . internalListeners [ event ] = callback
233+ this . internalListeners [ event ] = callback ;
231234 }
232235
233236 bind ( channel : Channel , event : string , callback : Function ) : void {
234237 if ( ! this . listeners [ channel . name ] ) {
235- this . listeners [ channel . name ] = { }
238+ this . listeners [ channel . name ] = { } ;
236239 }
237240
238- this . listeners [ channel . name ] [ event ] = callback
241+ this . listeners [ channel . name ] [ event ] = callback ;
239242 }
240243
241244 unbindEvent ( channel : Channel , event : string , callback : Function = null ) : void {
242245 if ( this . internalListeners [ event ] && ( callback === null || this . internalListeners [ event ] === callback ) ) {
243- delete this . internalListeners [ event ]
246+ delete this . internalListeners [ event ] ;
244247 }
245248 }
246249}
0 commit comments