1
1
import { AxiosResponse } from "axios" ;
2
- import axios from 'axios' ;
3
2
import { Channel } from "./Channel" ;
4
3
5
4
export type Options = { authEndpoint : string , host : string , bearerToken : string , auth : any , debug : boolean } ;
6
5
7
6
export type MessageBody = { event : string , channel ?: string , data : object } ;
8
7
8
+ const LOG_PREFIX = '[AG-WS]' ;
9
+
9
10
export class Websocket {
10
11
buffer : Array < object > = [ ] ;
11
12
12
13
options : Options ;
13
14
14
15
websocket : WebSocket ;
15
16
16
- private listeners : { [ channelName : string ] : { [ eventName : string ] : Function } } = { }
17
+ private listeners : { [ channelName : string ] : { [ eventName : string ] : Function } } = { } ;
17
18
18
19
private internalListeners : { [ eventName : string ] : Function } = { } ;
19
20
@@ -27,157 +28,162 @@ export class Websocket {
27
28
private pingInterval : NodeJS . Timeout ;
28
29
29
30
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...') ;
31
32
32
- this . websocket = new WebSocket ( host )
33
+ this . websocket = new WebSocket ( host ) ;
33
34
34
35
this . websocket . onerror = ( ) => {
36
+
35
37
if ( ! this . hasConnected ) {
38
+
36
39
setTimeout ( ( ) => {
37
- this . socketId = undefined
38
- this . connect ( host )
40
+ this . socketId = undefined ;
41
+ this . connect ( host ) ;
39
42
} , 3000 ) ;
40
43
}
41
- }
44
+ } ;
42
45
43
46
this . websocket . onopen = ( ) => {
44
- this . options . debug && console . log ( ' Connected !') ;
47
+ this . options . debug && console . log ( LOG_PREFIX + ' Connected !') ;
45
48
this . hasConnected = true ;
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 )
56
- }
59
+ this . buffer . splice ( 0 , 1 ) ;
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
- 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 ) ;
63
67
64
68
if ( ! message ) {
65
- return
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
- this . listeners [ message . channel ] [ message . event ] ( message . data )
76
+ this . listeners [ message . channel ] [ message . event ] ( message . data ) ;
73
77
}
74
78
75
- return
79
+ return ;
76
80
}
77
81
78
82
if ( this . internalListeners [ message . event ] ) {
79
- this . internalListeners [ message . event ] ( message . data )
83
+ this . internalListeners [ message . event ] ( message . data ) ;
80
84
}
81
85
}
82
86
83
87
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' ) ;
92
+
88
93
this . send ( {
89
94
event : 'ping' ,
90
- } )
95
+ } ) ;
91
96
}
92
- } , 60 * 1000 )
97
+ } , 60 * 1000 ) ;
93
98
}
94
99
95
100
96
101
this . websocket . onclose = ( ) => {
97
102
this . options . debug && console . info ( 'Connection closed.' ) ;
103
+
98
104
if ( this . closing ) {
99
105
return ;
100
106
}
101
- this . hasConnected = false
107
+
108
+ this . hasConnected = false ;
102
109
this . options . debug && console . info ( 'Connection lost, reconnecting...' ) ;
110
+
103
111
setTimeout ( ( ) => {
104
- this . socketId = undefined
105
- this . connect ( host )
112
+ this . socketId = undefined ;
113
+ this . connect ( host ) ;
106
114
} , 1000 ) ;
107
115
} ;
108
116
109
117
this . on ( 'whoami' , ( { socket_id : socketId } ) => {
110
- this . socketId = socketId
118
+ this . socketId = socketId ;
111
119
112
- this . options . debug && console . log ( `just set socketId to ${ socketId } ` )
120
+ this . options . debug && console . log ( `${ LOG_PREFIX } Just set socketId to ${ socketId } ` ) ;
113
121
114
122
// Handle the backlog and don't empty it, we'll need it if we lose connection
115
123
let channel : Channel ;
124
+
116
125
for ( channel of this . channelBacklog ) {
117
- this . actuallySubscribe ( channel )
126
+ this . actuallySubscribe ( channel ) ;
118
127
}
119
-
120
- } )
121
-
122
-
128
+ } ) ;
123
129
}
124
130
125
131
constructor ( options : Options ) {
126
132
this . options = options ;
127
133
128
134
this . connect ( this . options . host ) ;
129
135
130
- return this
136
+ return this ;
131
137
}
132
138
133
139
protected parseMessage ( body : string ) : MessageBody {
134
140
try {
135
- return JSON . parse ( body )
141
+ return JSON . parse ( body ) ;
136
142
} catch ( error ) {
137
- this . options . debug && console . error ( error )
143
+ this . options . debug && console . error ( error ) ;
138
144
139
- return undefined
145
+ return undefined ;
140
146
}
141
147
}
142
148
143
149
getSocketId ( ) : string {
144
- return this . socketId
150
+ return this . socketId ;
145
151
}
146
152
147
153
private socketIsReady ( ) : boolean {
148
- return this . websocket . readyState === this . websocket . OPEN
154
+ return this . websocket . readyState === this . websocket . OPEN ;
149
155
}
150
156
151
157
send ( message : object ) : void {
152
158
if ( this . socketIsReady ( ) ) {
153
- this . websocket . send ( JSON . stringify ( message ) )
154
- return
159
+ this . websocket . send ( JSON . stringify ( message ) ) ;
160
+ return ;
155
161
}
156
162
157
- this . buffer . push ( message )
163
+ this . buffer . push ( message ) ;
158
164
}
159
165
160
166
close ( ) : void {
161
- this . closing = true
162
- this . internalListeners = { }
167
+ this . closing = true ;
168
+ this . internalListeners = { } ;
163
169
164
- clearInterval ( this . pingInterval )
165
- this . pingInterval = undefined
170
+ clearInterval ( this . pingInterval ) ;
171
+ this . pingInterval = undefined ;
166
172
167
- this . websocket . close ( )
173
+ this . websocket . close ( ) ;
168
174
}
169
175
170
176
subscribe ( channel : Channel ) : void {
171
177
if ( this . getSocketId ( ) ) {
172
- this . actuallySubscribe ( channel )
178
+ this . actuallySubscribe ( channel ) ;
173
179
} else {
174
- this . channelBacklog . push ( channel )
180
+ this . channelBacklog . push ( channel ) ;
175
181
}
176
182
}
177
183
178
184
private actuallySubscribe ( channel : Channel ) : void {
179
185
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 } ` ) ;
181
187
182
188
if ( this . options . bearerToken ) {
183
189
this . options . auth . headers [ 'Authorization' ] = 'Bearer ' + this . options . bearerToken ;
@@ -189,28 +195,28 @@ export class Websocket {
189
195
} , {
190
196
headers : this . options . auth . headers || { }
191
197
} ) . 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 } ` ) ;
193
199
194
200
this . send ( {
195
201
event : 'subscribe' ,
196
202
data : {
197
203
channel : channel . name ,
198
204
...response . data
199
205
} ,
200
- } )
206
+ } ) ;
201
207
} ) . 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 ) ;
204
210
} )
205
211
} 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 } ` ) ;
207
213
208
214
this . send ( {
209
215
event : 'subscribe' ,
210
216
data : {
211
217
channel : channel . name ,
212
218
} ,
213
- } )
219
+ } ) ;
214
220
}
215
221
}
216
222
@@ -220,28 +226,28 @@ export class Websocket {
220
226
data : {
221
227
channel : channel . name ,
222
228
} ,
223
- } )
229
+ } ) ;
224
230
225
231
if ( this . listeners [ channel . name ] ) {
226
- delete this . listeners [ channel . name ]
232
+ delete this . listeners [ channel . name ] ;
227
233
}
228
234
}
229
235
230
236
on ( event : string , callback : Function = null ) : void {
231
- this . internalListeners [ event ] = callback
237
+ this . internalListeners [ event ] = callback ;
232
238
}
233
239
234
240
bind ( channel : Channel , event : string , callback : Function ) : void {
235
241
if ( ! this . listeners [ channel . name ] ) {
236
- this . listeners [ channel . name ] = { }
242
+ this . listeners [ channel . name ] = { } ;
237
243
}
238
244
239
- this . listeners [ channel . name ] [ event ] = callback
245
+ this . listeners [ channel . name ] [ event ] = callback ;
240
246
}
241
247
242
248
unbindEvent ( channel : Channel , event : string , callback : Function = null ) : void {
243
249
if ( this . internalListeners [ event ] && ( callback === null || this . internalListeners [ event ] === callback ) ) {
244
- delete this . internalListeners [ event ]
250
+ delete this . internalListeners [ event ] ;
245
251
}
246
252
}
247
253
}
0 commit comments