@resolveio/client-lib-core 15.1.0 → 15.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -232,6 +232,7 @@ class SocketService {
232
232
  this.onconnecting = function () { };
233
233
  this.onmessage = function (event) { };
234
234
  this.onerror = function (event) { };
235
+ document.addEventListener('visibilitychange', this.handleVisibilityChange.bind(this));
235
236
  }
236
237
  openSocket(environment, protocols) {
237
238
  this.url = environment.WS_URL;
@@ -272,7 +273,7 @@ class SocketService {
272
273
  this.log(new Date(), 'WS', 'onclose', this.url);
273
274
  this.onclose(event);
274
275
  setTimeout(() => {
275
- this.connect();
276
+ this.reconnect();
276
277
  }, this.reconnectInterval);
277
278
  this.stopPinging();
278
279
  };
@@ -290,7 +291,7 @@ class SocketService {
290
291
  this.ws.onerror = (event) => {
291
292
  this.log(new Date(), 'WS', 'onerror', this, event);
292
293
  this.onerror(event);
293
- this.reconnect();
294
+ this.close();
294
295
  };
295
296
  }
296
297
  }
@@ -316,13 +317,16 @@ class SocketService {
316
317
  }
317
318
  startPinging() {
318
319
  this.pingInterval = setInterval(() => {
319
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
320
+ if (this.isWebSocketActive()) {
320
321
  this.send('ping');
321
322
  this.pongTimeout = setTimeout(() => {
322
323
  this.log(new Date(), 'WS', 'pong not received, closing connection');
323
324
  this.close();
324
325
  }, this.pongTimeoutTime);
325
326
  }
327
+ else {
328
+ this.reconnect();
329
+ }
326
330
  }, this.pingIntervalTime);
327
331
  }
328
332
  stopPinging() {
@@ -348,7 +352,7 @@ class SocketService {
348
352
  }
349
353
  }
350
354
  send(...data) {
351
- if (this.ws && this.ws.readyState === this.ws.OPEN) {
355
+ if (this.isWebSocketActive()) {
352
356
  data.forEach(item => this.convertDatesToUTC(item));
353
357
  this.ws.send(JSON.stringify(data));
354
358
  return true;
@@ -371,12 +375,12 @@ class SocketService {
371
375
  }
372
376
  reconnect() {
373
377
  this.log(new Date(), 'WS', 'reconnect', this.readyState);
374
- if (this.readyState === WebSocket.OPEN) {
375
- this.close();
376
- }
377
- else if (this.readyState === WebSocket.CLOSING || this.readyState === WebSocket.CLOSED) {
378
+ if (this.readyState === WebSocket.CLOSED) {
378
379
  this.connect();
379
380
  }
381
+ else if (this.readyState !== WebSocket.CONNECTING) {
382
+ this.close();
383
+ }
380
384
  }
381
385
  getBufferAmount() {
382
386
  if (this.ws) {
@@ -391,6 +395,16 @@ class SocketService {
391
395
  console.log(...args);
392
396
  }
393
397
  }
398
+ handleVisibilityChange() {
399
+ if (document.visibilityState === 'visible') {
400
+ if (!this.isWebSocketActive()) {
401
+ this.reconnect();
402
+ }
403
+ }
404
+ }
405
+ isWebSocketActive() {
406
+ return this.ws && this.ws.readyState === WebSocket.OPEN;
407
+ }
394
408
  }
395
409
  SocketService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: SocketService, deps: [{ token: i1$2.LocalStorageService }], target: i0.ɵɵFactoryTarget.Injectable });
396
410
  SocketService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: SocketService });