@@ -5,14 +5,16 @@ export type Options = { authEndpoint: string, host: string, bearerToken: string,
55
66export type MessageBody = { event : string , channel ?: string , data : object } ;
77
8+ const LOG_PREFIX = '[AG-WS] ' ;
9+
810export class Websocket {
911 buffer : Array < object > = [ ] ;
1012
1113 options : Options ;
1214
1315 websocket : WebSocket ;
1416
15- private listeners : { [ channelName : string ] : { [ eventName : string ] : Function } } = { }
17+ private listeners : { [ channelName : string ] : { [ eventName : string ] : Function } } = { } ;
1618
1719 private internalListeners : { [ eventName : string ] : Function } = { } ;
1820
@@ -26,12 +28,14 @@ export class Websocket {
2628 private pingInterval : NodeJS . Timeout ;
2729
2830 private connect ( host : string ) : void {
29- this . options . debug && console . log ( 'Trying to connect...' ) ;
31+ this . options . debug && console . log ( LOG_PREFIX + 'Trying to connect...' ) ;
3032
3133 this . websocket = new WebSocket ( host ) ;
3234
3335 this . websocket . onerror = ( ) => {
36+
3437 if ( ! this . hasConnected ) {
38+
3539 setTimeout ( ( ) => {
3640 this . socketId = undefined ;
3741 this . connect ( host ) ;
@@ -40,33 +44,33 @@ export class Websocket {
4044 } ;
4145
4246 this . websocket . onopen = ( ) => {
43- this . options . debug && console . log ( 'Connected !' ) ;
47+ this . options . debug && console . log ( LOG_PREFIX + 'Connected !' ) ;
4448 this . hasConnected = true ;
4549
4650 this . send ( {
4751 event : 'whoami' ,
4852 } ) ;
4953
5054 while ( this . buffer . length ) {
51- const message = this . buffer [ 0 ]
55+ const message = this . buffer [ 0 ] ;
5256
53- this . send ( message )
57+ this . send ( message ) ;
5458
55- this . buffer . splice ( 0 , 1 )
59+ this . buffer . splice ( 0 , 1 ) ;
5660 } ;
5761
5862 // Register events only once connected, or they won't be registered if connection failed/lost
5963
6064 this . websocket . onmessage = ( messageEvent : MessageEvent ) => {
6165 const message = this . parseMessage ( messageEvent . data ) ;
62- this . options . debug && console . log ( 'onmessage' , messageEvent . data ) ;
66+ this . options . debug && console . log ( LOG_PREFIX + 'onmessage' , messageEvent . data ) ;
6367
6468 if ( ! message ) {
6569 return ;
6670 }
6771
6872 if ( message . channel ) {
69- this . options . debug && console . log ( `Received event ${ message . event } on channel ${ message . channel } ` ) ;
73+ this . options . debug && console . log ( `${ LOG_PREFIX } Received event ${ message . event } on channel ${ message . channel } ` ) ;
7074
7175 if ( this . listeners [ message . channel ] && this . listeners [ message . channel ] [ message . event ] ) {
7276 this . listeners [ message . channel ] [ message . event ] ( message . data ) ;
@@ -84,7 +88,7 @@ export class Websocket {
8488 // send ping every 60 seconds to keep connection alive
8589 this . pingInterval = setInterval ( ( ) => {
8690 if ( this . websocket . readyState === this . websocket . OPEN ) {
87- this . options . debug && console . log ( 'Sending ping' ) ;
91+ this . options . debug && console . log ( LOG_PREFIX + 'Sending ping' ) ;
8892
8993 this . send ( {
9094 event : 'ping' ,
@@ -113,7 +117,7 @@ export class Websocket {
113117 this . on ( 'whoami' , ( { socket_id : socketId } ) => {
114118 this . socketId = socketId ;
115119
116- this . options . debug && console . log ( `just set socketId to ${ socketId } ` ) ;
120+ this . options . debug && console . log ( `${ LOG_PREFIX } just set socketId to ${ socketId } ` ) ;
117121
118122 // Handle the backlog and don't empty it, we'll need it if we lose connection
119123 let channel : Channel ;
@@ -179,7 +183,7 @@ export class Websocket {
179183
180184 private actuallySubscribe ( channel : Channel ) : void {
181185 if ( channel . name . startsWith ( 'private-' ) || channel . name . startsWith ( 'presence-' ) ) {
182- this . options . debug && console . log ( `Sending auth request for channel ${ channel . name } ` ) ;
186+ this . options . debug && console . log ( `${ LOG_PREFIX } Sending auth request for channel ${ channel . name } ` ) ;
183187
184188 if ( this . options . bearerToken ) {
185189 this . options . auth . headers [ 'Authorization' ] = 'Bearer ' + this . options . bearerToken ;
@@ -191,7 +195,7 @@ export class Websocket {
191195 } , {
192196 headers : this . options . auth . headers || { }
193197 } ) . then ( ( response : AxiosResponse ) => {
194- this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` ) ;
198+ this . options . debug && console . log ( `${ LOG_PREFIX } Subscribing to channels ${ channel . name } ` ) ;
195199
196200 this . send ( {
197201 event : 'subscribe' ,
@@ -201,11 +205,11 @@ export class Websocket {
201205 } ,
202206 } ) ;
203207 } ) . catch ( ( error ) => {
204- this . options . debug && console . log ( `Auth request for channel ${ channel . name } failed` ) ;
208+ this . options . debug && console . log ( `${ LOG_PREFIX } Auth request for channel ${ channel . name } failed` ) ;
205209 this . options . debug && console . error ( error ) ;
206210 } )
207211 } else {
208- this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` ) ;
212+ this . options . debug && console . log ( `${ LOG_PREFIX } Subscribing to channels ${ channel . name } ` ) ;
209213
210214 this . send ( {
211215 event : 'subscribe' ,
0 commit comments