@resolveio/client-lib-core 1.3.20 → 1.4.0

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 = 15000; // 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));
@@ -1203,18 +1234,11 @@ class SocketManagerService {
1203
1234
  if (!this._runningQueue) {
1204
1235
  if (this._sendQueue.length) {
1205
1236
  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;
1237
+ if (this._socket && this._socket.ws && this.socketStatus === WebSocket.OPEN) {
1238
+ let nextSendItem = this._sendQueue[this._sendQueue.length - 1];
1239
+ let sendStatus = this._socket.send(...nextSendItem);
1240
+ if (sendStatus) {
1241
+ this._sendQueue.pop();
1218
1242
  }
1219
1243
  }
1220
1244
  this._runningQueue = false;