11import { AxiosResponse } from "axios" ;
2- import axios from 'axios' ;
32import { Channel } from "./Channel" ;
43
54export type Options = { authEndpoint : string , host : string , bearerToken : string , auth : any , debug : boolean } ;
65
76export type MessageBody = { event : string , channel ?: string , data : object } ;
87
8+ const LOG_PREFIX = '[AG-WS]' ;
9+
910export class Websocket {
1011 buffer : Array < object > = [ ] ;
1112
1213 options : Options ;
1314
1415 websocket : WebSocket ;
1516
16- private listeners : { [ channelName : string ] : { [ eventName : string ] : Function } } = { }
17+ private listeners : { [ channelName : string ] : { [ eventName : string ] : Function } } = { } ;
1718
1819 private internalListeners : { [ eventName : string ] : Function } = { } ;
1920
@@ -27,157 +28,162 @@ export class Websocket {
2728 private pingInterval : NodeJS . Timeout ;
2829
2930 private connect ( host : string ) : void {
30- this . options . debug && console . log ( ' Trying to connect...') ;
31+ this . options . debug && console . log ( LOG_PREFIX + ' Trying to connect...') ;
3132
32- this . websocket = new WebSocket ( host )
33+ this . websocket = new WebSocket ( host ) ;
3334
3435 this . websocket . onerror = ( ) => {
36+
3537 if ( ! this . hasConnected ) {
38+
3639 setTimeout ( ( ) => {
37- this . socketId = undefined
38- this . connect ( host )
40+ this . socketId = undefined ;
41+ this . connect ( host ) ;
3942 } , 3000 ) ;
4043 }
41- }
44+ } ;
4245
4346 this . websocket . onopen = ( ) => {
44- this . options . debug && console . log ( ' Connected !') ;
47+ this . options . debug && console . log ( LOG_PREFIX + ' Connected !') ;
4548 this . hasConnected = true ;
49+
4650 this . send ( {
4751 event : 'whoami' ,
48- } )
52+ } ) ;
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 )
56- }
59+ this . buffer . splice ( 0 , 1 ) ;
60+ } ;
5761
5862 // Register events only once connected, or they won't be registered if connection failed/lost
5963
6064 this . websocket . onmessage = ( messageEvent : MessageEvent ) => {
61- const message = this . parseMessage ( messageEvent . data )
62- this . options . debug && console . log ( ' onmessage', messageEvent . data )
65+ const message = this . parseMessage ( messageEvent . data ) ;
66+ this . options . debug && console . log ( LOG_PREFIX + ' onmessage', messageEvent . data ) ;
6367
6468 if ( ! message ) {
65- return
69+ 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 ] ) {
72- this . listeners [ message . channel ] [ message . event ] ( message . data )
76+ this . listeners [ message . channel ] [ message . event ] ( message . data ) ;
7377 }
7478
75- return
79+ return ;
7680 }
7781
7882 if ( this . internalListeners [ message . event ] ) {
79- this . internalListeners [ message . event ] ( message . data )
83+ this . internalListeners [ message . event ] ( message . data ) ;
8084 }
8185 }
8286
8387
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' ) ;
92+
8893 this . send ( {
8994 event : 'ping' ,
90- } )
95+ } ) ;
9196 }
92- } , 60 * 1000 )
97+ } , 60 * 1000 ) ;
9398 }
9499
95100
96101 this . websocket . onclose = ( ) => {
97102 this . options . debug && console . info ( 'Connection closed.' ) ;
103+
98104 if ( this . closing ) {
99105 return ;
100106 }
101- this . hasConnected = false
107+
108+ this . hasConnected = false ;
102109 this . options . debug && console . info ( 'Connection lost, reconnecting...' ) ;
110+
103111 setTimeout ( ( ) => {
104- this . socketId = undefined
105- this . connect ( host )
112+ this . socketId = undefined ;
113+ this . connect ( host ) ;
106114 } , 1000 ) ;
107115 } ;
108116
109117 this . on ( 'whoami' , ( { socket_id : socketId } ) => {
110- this . socketId = socketId
118+ this . socketId = socketId ;
111119
112- this . options . debug && console . log ( `just set socketId to ${ socketId } ` )
120+ this . options . debug && console . log ( `${ LOG_PREFIX } Just set socketId to ${ socketId } ` ) ;
113121
114122 // Handle the backlog and don't empty it, we'll need it if we lose connection
115123 let channel : Channel ;
124+
116125 for ( channel of this . channelBacklog ) {
117- this . actuallySubscribe ( channel )
126+ this . actuallySubscribe ( channel ) ;
118127 }
119-
120- } )
121-
122-
128+ } ) ;
123129 }
124130
125131 constructor ( options : Options ) {
126132 this . options = options ;
127133
128134 this . connect ( this . options . host ) ;
129135
130- return this
136+ return this ;
131137 }
132138
133139 protected parseMessage ( body : string ) : MessageBody {
134140 try {
135- return JSON . parse ( body )
141+ return JSON . parse ( body ) ;
136142 } catch ( error ) {
137- this . options . debug && console . error ( error )
143+ this . options . debug && console . error ( error ) ;
138144
139- return undefined
145+ return undefined ;
140146 }
141147 }
142148
143149 getSocketId ( ) : string {
144- return this . socketId
150+ return this . socketId ;
145151 }
146152
147153 private socketIsReady ( ) : boolean {
148- return this . websocket . readyState === this . websocket . OPEN
154+ return this . websocket . readyState === this . websocket . OPEN ;
149155 }
150156
151157 send ( message : object ) : void {
152158 if ( this . socketIsReady ( ) ) {
153- this . websocket . send ( JSON . stringify ( message ) )
154- return
159+ this . websocket . send ( JSON . stringify ( message ) ) ;
160+ return ;
155161 }
156162
157- this . buffer . push ( message )
163+ this . buffer . push ( message ) ;
158164 }
159165
160166 close ( ) : void {
161- this . closing = true
162- this . internalListeners = { }
167+ this . closing = true ;
168+ this . internalListeners = { } ;
163169
164- clearInterval ( this . pingInterval )
165- this . pingInterval = undefined
170+ clearInterval ( this . pingInterval ) ;
171+ this . pingInterval = undefined ;
166172
167- this . websocket . close ( )
173+ this . websocket . close ( ) ;
168174 }
169175
170176 subscribe ( channel : Channel ) : void {
171177 if ( this . getSocketId ( ) ) {
172- this . actuallySubscribe ( channel )
178+ this . actuallySubscribe ( channel ) ;
173179 } else {
174- this . channelBacklog . push ( channel )
180+ this . channelBacklog . push ( channel ) ;
175181 }
176182 }
177183
178184 private actuallySubscribe ( channel : Channel ) : void {
179185 if ( channel . name . startsWith ( 'private-' ) || channel . name . startsWith ( 'presence-' ) ) {
180- 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 } ` ) ;
181187
182188 if ( this . options . bearerToken ) {
183189 this . options . auth . headers [ 'Authorization' ] = 'Bearer ' + this . options . bearerToken ;
@@ -189,28 +195,28 @@ export class Websocket {
189195 } , {
190196 headers : this . options . auth . headers || { }
191197 } ) . then ( ( response : AxiosResponse ) => {
192- this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` )
198+ this . options . debug && console . log ( `${ LOG_PREFIX } Subscribing to private channel ${ channel . name } ` ) ;
193199
194200 this . send ( {
195201 event : 'subscribe' ,
196202 data : {
197203 channel : channel . name ,
198204 ...response . data
199205 } ,
200- } )
206+ } ) ;
201207 } ) . catch ( ( error ) => {
202- this . options . debug && console . log ( `Auth request for channel ${ channel . name } failed` )
203- this . options . debug && console . error ( error )
208+ this . options . debug && console . log ( `${ LOG_PREFIX } Auth request for channel ${ channel . name } failed` ) ;
209+ this . options . debug && console . error ( error ) ;
204210 } )
205211 } else {
206- this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` )
212+ this . options . debug && console . log ( `${ LOG_PREFIX } Subscribing to channel ${ channel . name } ` ) ;
207213
208214 this . send ( {
209215 event : 'subscribe' ,
210216 data : {
211217 channel : channel . name ,
212218 } ,
213- } )
219+ } ) ;
214220 }
215221 }
216222
@@ -220,28 +226,28 @@ export class Websocket {
220226 data : {
221227 channel : channel . name ,
222228 } ,
223- } )
229+ } ) ;
224230
225231 if ( this . listeners [ channel . name ] ) {
226- delete this . listeners [ channel . name ]
232+ delete this . listeners [ channel . name ] ;
227233 }
228234 }
229235
230236 on ( event : string , callback : Function = null ) : void {
231- this . internalListeners [ event ] = callback
237+ this . internalListeners [ event ] = callback ;
232238 }
233239
234240 bind ( channel : Channel , event : string , callback : Function ) : void {
235241 if ( ! this . listeners [ channel . name ] ) {
236- this . listeners [ channel . name ] = { }
242+ this . listeners [ channel . name ] = { } ;
237243 }
238244
239- this . listeners [ channel . name ] [ event ] = callback
245+ this . listeners [ channel . name ] [ event ] = callback ;
240246 }
241247
242248 unbindEvent ( channel : Channel , event : string , callback : Function = null ) : void {
243249 if ( this . internalListeners [ event ] && ( callback === null || this . internalListeners [ event ] === callback ) ) {
244- delete this . internalListeners [ event ]
250+ delete this . internalListeners [ event ] ;
245251 }
246252 }
247253}
0 commit comments