@@ -5,14 +5,16 @@ export type Options = { authEndpoint: string, host: string, bearerToken: string,
5
5
6
6
export type MessageBody = { event : string , channel ?: string , data : object } ;
7
7
8
+ const LOG_PREFIX = '[AG-WS] ' ;
9
+
8
10
export class Websocket {
9
11
buffer : Array < object > = [ ] ;
10
12
11
13
options : Options ;
12
14
13
15
websocket : WebSocket ;
14
16
15
- private listeners : { [ channelName : string ] : { [ eventName : string ] : Function } } = { }
17
+ private listeners : { [ channelName : string ] : { [ eventName : string ] : Function } } = { } ;
16
18
17
19
private internalListeners : { [ eventName : string ] : Function } = { } ;
18
20
@@ -26,12 +28,14 @@ export class Websocket {
26
28
private pingInterval : NodeJS . Timeout ;
27
29
28
30
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...' ) ;
30
32
31
33
this . websocket = new WebSocket ( host ) ;
32
34
33
35
this . websocket . onerror = ( ) => {
36
+
34
37
if ( ! this . hasConnected ) {
38
+
35
39
setTimeout ( ( ) => {
36
40
this . socketId = undefined ;
37
41
this . connect ( host ) ;
@@ -40,33 +44,33 @@ export class Websocket {
40
44
} ;
41
45
42
46
this . websocket . onopen = ( ) => {
43
- this . options . debug && console . log ( 'Connected !' ) ;
47
+ this . options . debug && console . log ( LOG_PREFIX + 'Connected !' ) ;
44
48
this . hasConnected = true ;
45
49
46
50
this . send ( {
47
51
event : 'whoami' ,
48
52
} ) ;
49
53
50
54
while ( this . buffer . length ) {
51
- const message = this . buffer [ 0 ]
55
+ const message = this . buffer [ 0 ] ;
52
56
53
- this . send ( message )
57
+ this . send ( message ) ;
54
58
55
- this . buffer . splice ( 0 , 1 )
59
+ this . buffer . splice ( 0 , 1 ) ;
56
60
} ;
57
61
58
62
// Register events only once connected, or they won't be registered if connection failed/lost
59
63
60
64
this . websocket . onmessage = ( messageEvent : MessageEvent ) => {
61
65
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 ) ;
63
67
64
68
if ( ! message ) {
65
69
return ;
66
70
}
67
71
68
72
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 } ` ) ;
70
74
71
75
if ( this . listeners [ message . channel ] && this . listeners [ message . channel ] [ message . event ] ) {
72
76
this . listeners [ message . channel ] [ message . event ] ( message . data ) ;
@@ -84,7 +88,7 @@ export class Websocket {
84
88
// send ping every 60 seconds to keep connection alive
85
89
this . pingInterval = setInterval ( ( ) => {
86
90
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' ) ;
88
92
89
93
this . send ( {
90
94
event : 'ping' ,
@@ -113,7 +117,7 @@ export class Websocket {
113
117
this . on ( 'whoami' , ( { socket_id : socketId } ) => {
114
118
this . socketId = socketId ;
115
119
116
- this . options . debug && console . log ( `just set socketId to ${ socketId } ` ) ;
120
+ this . options . debug && console . log ( `${ LOG_PREFIX } just set socketId to ${ socketId } ` ) ;
117
121
118
122
// Handle the backlog and don't empty it, we'll need it if we lose connection
119
123
let channel : Channel ;
@@ -179,7 +183,7 @@ export class Websocket {
179
183
180
184
private actuallySubscribe ( channel : Channel ) : void {
181
185
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 } ` ) ;
183
187
184
188
if ( this . options . bearerToken ) {
185
189
this . options . auth . headers [ 'Authorization' ] = 'Bearer ' + this . options . bearerToken ;
@@ -191,7 +195,7 @@ export class Websocket {
191
195
} , {
192
196
headers : this . options . auth . headers || { }
193
197
} ) . 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 } ` ) ;
195
199
196
200
this . send ( {
197
201
event : 'subscribe' ,
@@ -201,11 +205,11 @@ export class Websocket {
201
205
} ,
202
206
} ) ;
203
207
} ) . 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` ) ;
205
209
this . options . debug && console . error ( error ) ;
206
210
} )
207
211
} 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 } ` ) ;
209
213
210
214
this . send ( {
211
215
event : 'subscribe' ,
0 commit comments