1
1
/*!
2
- * Async Client Websocket v1.2.5
2
+ * Async Client Websocket v1.2.8
3
3
* by Jean Kássio
4
4
*
5
5
* More info:
@@ -64,7 +64,7 @@ class WebSocketClient{
64
64
}
65
65
66
66
if ( this . queueLocalStorage ) {
67
- loadQueue ( ) ;
67
+ this . loadQueue ( ) ;
68
68
}
69
69
70
70
while ( this . messageQueue . length > 0 ) {
@@ -86,25 +86,25 @@ class WebSocketClient{
86
86
87
87
this . _log ( "Received Message:" , message ) ;
88
88
89
- if ( this . incomingMiddleware ) {
90
- message = this . incomingMiddleware ( message ) ;
91
- this . _log ( "Incoming Middleware return:" , message ) ;
92
- }
93
-
94
- if ( this . onMessage ) {
89
+ if ( this . recordLatency && message === this . pong ) {
90
+
91
+ this . latencyTimestamp = Date . now ( ) ;
92
+ this . latency = this . latencyTimestamp - this . lastPingTimestamp ;
93
+
94
+ if ( this . onLatency ) {
95
+ this . onLatency ( this . latency ) ;
96
+ }
97
+
98
+ this . _log ( "Calculated latency:" , this . latency ) ;
99
+
100
+ } else {
101
+
102
+ if ( this . incomingMiddleware ) {
103
+ message = this . incomingMiddleware ( message ) ;
104
+ this . _log ( "Incoming Middleware return:" , message ) ;
105
+ }
95
106
96
- if ( this . recordLatency && message === this . pong ) {
97
-
98
- this . latencyTimestamp = Date . now ( ) ;
99
- this . latency = this . latencyTimestamp - this . lastPingTimestamp ;
100
-
101
- if ( this . onLatency ) {
102
- this . onLatency ( this . latency ) ;
103
- }
104
-
105
- this . _log ( "Calculated latency:" , this . latency ) ;
106
-
107
- } else {
107
+ if ( this . onMessage ) {
108
108
this . onMessage ( message ) ;
109
109
}
110
110
@@ -114,6 +114,10 @@ class WebSocketClient{
114
114
115
115
this . socket . onerror = ( error ) => {
116
116
117
+ if ( timer ) {
118
+ clearTimeout ( timer ) ;
119
+ }
120
+
117
121
if ( this . onError ) {
118
122
this . onError ( error ) ;
119
123
}
@@ -126,6 +130,10 @@ class WebSocketClient{
126
130
127
131
this . socket . onclose = ( event ) => {
128
132
133
+ if ( timer ) {
134
+ clearTimeout ( timer ) ;
135
+ }
136
+
129
137
if ( this . onClose ) {
130
138
this . onClose ( event ) ;
131
139
}
@@ -134,6 +142,8 @@ class WebSocketClient{
134
142
135
143
this . _attemptReconnect ( ) ;
136
144
145
+ reject ( ) ;
146
+
137
147
} ;
138
148
139
149
} ) ;
@@ -177,7 +187,7 @@ class WebSocketClient{
177
187
this . messageQueue . push ( message ) ;
178
188
179
189
if ( this . queueLocalStorage ) {
180
- saveQueue ( this . messageQueue ) ;
190
+ this . saveQueue ( this . messageQueue ) ;
181
191
}
182
192
183
193
this . _log ( "Websocket is not open! Putting message in queue" ) ;
@@ -247,6 +257,10 @@ class WebSocketClient{
247
257
clearInterval ( this . keepAliveInterval ) ;
248
258
}
249
259
260
+ if ( this . recordLatency ) {
261
+ return ;
262
+ }
263
+
250
264
this . keepAliveInterval = setInterval ( ( ) => {
251
265
252
266
this . _log ( "Send ping to KeepAlive" ) ;
@@ -294,14 +308,12 @@ class WebSocketClient{
294
308
295
309
}
296
310
297
- trackLatency ( ping , pong , interval = 10000 ) {
311
+ trackLatency ( interval = 10000 , ping , pong ) {
298
312
299
313
this . _log ( "Starting track latency" ) ;
300
314
301
315
this . recordLatency = true ;
302
316
303
- this . lastPingTimestamp = Date . now ( ) ;
304
-
305
317
if ( ping ) {
306
318
this . ping = ping ;
307
319
this . _log ( "Set ping name:" , ping ) ;
@@ -312,16 +324,24 @@ class WebSocketClient{
312
324
this . _log ( "Set pong name:" , pong ) ;
313
325
}
314
326
327
+ this . _trackLatency ( ) ;
328
+
315
329
this . pingInterval = setInterval ( ( ) => {
316
330
317
- this . lastPingTimestamp = Date . now ( ) ;
318
- this . send ( this . ping ) ;
319
- this . _log ( "Send ping to latency:" , this . ping ) ;
331
+ this . _trackLatency ( ) ;
320
332
321
333
} , interval ) ;
322
334
323
335
}
324
336
337
+ _trackLatency ( ) {
338
+
339
+ this . lastPingTimestamp = Date . now ( ) ;
340
+ this . send ( this . ping ) ;
341
+ this . _log ( "Send ping to latency:" , this . ping ) ;
342
+
343
+ }
344
+
325
345
autoReconnect ( interval = 3000 , maxAttemps = Infinity , callback = null ) {
326
346
327
347
this . _autoReconnect = true ;
@@ -369,7 +389,7 @@ class WebSocketClient{
369
389
370
390
}
371
391
372
- _attemptReconnect ( ) {
392
+ async _attemptReconnect ( ) {
373
393
374
394
if ( ! this . _autoReconnect || this . reconnectAttemps >= this . maxReconnectAttemps ) {
375
395
return ;
@@ -379,22 +399,15 @@ class WebSocketClient{
379
399
380
400
this . connect ( ) . then ( ( ) => {
381
401
382
- this . _log ( "Websocket reconnected successfully" ) ;
402
+ this . _log ( "[WebSocket] Reconnected successfully" ) ;
383
403
384
404
this . reconnectAttemps = 0 ;
385
405
386
406
if ( this . reconnectCallback ) {
387
407
this . reconnectCallback ( ) ;
388
- this . _log ( "Reconnect callback was called" ) ;
408
+ this . _log ( "[WebSocket] Reconnect callback was called" ) ;
389
409
}
390
410
391
- } ) . catch ( ( ) => {
392
-
393
- this . _log ( "Error reconnecting Websocket" ) ;
394
-
395
- this . reconnectAttemps ++ ;
396
- this . _attemptReconnect ( ) ;
397
-
398
411
} ) ;
399
412
400
413
} , this . reconnectInterval ) ;
0 commit comments