@iam4x/reconnecting-websocket 1.3.0 → 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.
Files changed (2) hide show
  1. package/dist/index.js +20 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -180,9 +180,28 @@ export class ReconnectingWebSocket {
180
180
  if (this.forcedClose) {
181
181
  return;
182
182
  }
183
- // Trigger reconnection due to inactivity
183
+ // Proactively trigger reconnection due to inactivity
184
+ // Don't rely on the close event as it may never fire on a stalled connection
184
185
  if (this.ws) {
186
+ // Stop health check to prevent it from also triggering reconnection
187
+ this.stopHealthCheck();
188
+ // Remove event listeners to prevent any late events from interfering
189
+ if (this.openFn)
190
+ this.ws.removeEventListener("open", this.openFn);
191
+ if (this.msgFn)
192
+ this.ws.removeEventListener("message", this.msgFn);
193
+ if (this.closeFn)
194
+ this.ws.removeEventListener("close", this.closeFn);
195
+ if (this.errorFn)
196
+ this.ws.removeEventListener("error", this.errorFn);
197
+ // Try to close the socket (may hang on stalled connections, but we don't wait)
185
198
  this.ws.close();
199
+ // Clear the socket reference
200
+ this.ws = undefined;
201
+ // Emit close event to listeners with a special code indicating inactivity timeout
202
+ this.emit("close", { code: 4000, reason: "Inactivity timeout" });
203
+ // Schedule reconnection directly without waiting for close event
204
+ this.scheduleReconnect();
186
205
  }
187
206
  }, this.options.watchingInactivityTimeout);
188
207
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "module": "src/index.ts",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
- "version": "1.3.0",
6
+ "version": "1.4.0",
7
7
  "private": false,
8
8
  "types": "./dist/index.d.ts",
9
9
  "exports": {