@resolveio/client-lib-core 1.3.20 → 1.4.1

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.
@@ -222,6 +222,10 @@ class SocketService {
222
222
  this.readyState$ = new BehaviorSubject(0);
223
223
  //List of WebSocket sub-protocols
224
224
  this.protocols = [];
225
+ this.pingInterval = null;
226
+ this.pongTimeout = null;
227
+ this.pingIntervalTime = 15000; // Time between each ping (e.g., 30 seconds)
228
+ this.pongTimeoutTime = 12000; // Time to wait for pong before closing connection (e.g., 10 seconds)
225
229
  //Set up the default 'noop' event handlers
226
230
  this.onopen = function (event) { };
227
231
  this.onclose = function (event) { };
@@ -255,6 +259,7 @@ class SocketService {
255
259
  this.readyState = WebSocket.OPEN;
256
260
  this.readyState$.next(this.readyState);
257
261
  this.onopen(event);
262
+ this.startPinging();
258
263
  };
259
264
  this.ws.onclose = (event) => {
260
265
  if (this.timeout) {
@@ -269,9 +274,16 @@ class SocketService {
269
274
  setTimeout(() => {
270
275
  this.connect();
271
276
  }, this.reconnectInterval);
277
+ this.stopPinging();
272
278
  };
273
279
  this.ws.onmessage = (event) => {
274
- this.onmessage(event);
280
+ if (event.data === 'pong') {
281
+ this.log(new Date(), 'WS', 'pong received');
282
+ clearTimeout(this.pongTimeout);
283
+ }
284
+ else {
285
+ this.onmessage(event);
286
+ }
275
287
  };
276
288
  this.ws.onerror = (event) => {
277
289
  this.log(new Date(), 'WS', 'onerror', this, event);
@@ -280,6 +292,25 @@ class SocketService {
280
292
  };
281
293
  }
282
294
  }
295
+ startPinging() {
296
+ this.pingInterval = setInterval(() => {
297
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {
298
+ this.ws.send(['ping'].toString()); // You can adjust this based on the server's expectations
299
+ this.pongTimeout = setTimeout(() => {
300
+ this.log(new Date(), 'WS', 'pong not received, closing connection');
301
+ this.close();
302
+ }, this.pongTimeoutTime);
303
+ }
304
+ }, this.pingIntervalTime);
305
+ }
306
+ stopPinging() {
307
+ if (this.pingInterval) {
308
+ clearInterval(this.pingInterval);
309
+ }
310
+ if (this.pongTimeout) {
311
+ clearTimeout(this.pongTimeout);
312
+ }
313
+ }
283
314
  send(...data) {
284
315
  if (this.ws && this.ws.readyState === this.ws.OPEN) {
285
316
  this.ws.send(JSON.stringify(data));
@@ -291,9 +322,6 @@ class SocketService {
291
322
  return false;
292
323
  }
293
324
  }
294
- /**
295
- * Returns boolean, whether websocket was FORCEFULLY closed.
296
- */
297
325
  close() {
298
326
  if (this.ws) {
299
327
  this.log(new Date(), 'WS', 'closing socket');
@@ -1203,18 +1231,11 @@ class SocketManagerService {
1203
1231
  if (!this._runningQueue) {
1204
1232
  if (this._sendQueue.length) {
1205
1233
  this._runningQueue = true;
1206
- for (let i = this._sendQueue.length - 1; i >= 0; i--) {
1207
- if (this._socket && this._socket.ws) {
1208
- if (this.socketStatus === WebSocket.OPEN) {
1209
- let nextSendItem = this._sendQueue.pop();
1210
- this._socket.send(...nextSendItem);
1211
- }
1212
- else {
1213
- break;
1214
- }
1215
- }
1216
- else {
1217
- break;
1234
+ if (this._socket && this._socket.ws) {
1235
+ let nextSendItem = this._sendQueue[this._sendQueue.length - 1];
1236
+ let sendStatus = this._socket.send(...nextSendItem);
1237
+ if (sendStatus) {
1238
+ this._sendQueue.pop();
1218
1239
  }
1219
1240
  }
1220
1241
  this._runningQueue = false;
@@ -1314,14 +1335,7 @@ class SocketManagerService {
1314
1335
  }
1315
1336
  send(...data) {
1316
1337
  if (data[3] === 'subscription') {
1317
- if (this.socketStatus === WebSocket.OPEN) {
1318
- this._socket.send(...data);
1319
- }
1320
- else {
1321
- if (!this._sendQueue.filter(a => JSON.stringify(a) === JSON.stringify(data)).length) {
1322
- this._sendQueue.splice(0, 0, data);
1323
- }
1324
- }
1338
+ this._socket.send(...data);
1325
1339
  }
1326
1340
  else if (data[3] === 'method') {
1327
1341
  if (this.socketStatus === WebSocket.OPEN) {
@@ -1406,9 +1420,7 @@ class SocketManagerService {
1406
1420
  }
1407
1421
  }
1408
1422
  else if (data[3] === 'offline') {
1409
- if (this.socketStatus === WebSocket.OPEN) {
1410
- this._socket.send(...data);
1411
- }
1423
+ this._socket.send(...data);
1412
1424
  }
1413
1425
  }
1414
1426
  getStatus() {