@emotion-machine/claw-messenger 0.1.5 → 0.1.6
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.
- package/dist/ws/client.js +18 -0
- package/package.json +1 -1
package/dist/ws/client.js
CHANGED
|
@@ -95,6 +95,17 @@ export class WsClient {
|
|
|
95
95
|
}
|
|
96
96
|
url.searchParams.set("key", this.opts.apiKey);
|
|
97
97
|
this.opts.log?.(`Connecting to ${url.origin}${url.pathname}...`);
|
|
98
|
+
// Close the previous socket if it's still open, so we don't leak
|
|
99
|
+
// connections on the server. Set this.ws to null first so the old
|
|
100
|
+
// socket's onclose handler sees it's stale and skips reconnect.
|
|
101
|
+
if (this.ws && this.ws.readyState !== WebSocket.CLOSED) {
|
|
102
|
+
const oldWs = this.ws;
|
|
103
|
+
this.ws = null;
|
|
104
|
+
try {
|
|
105
|
+
oldWs.close(1000, "Replaced by new connection");
|
|
106
|
+
}
|
|
107
|
+
catch { }
|
|
108
|
+
}
|
|
98
109
|
const ws = new WebSocket(url.toString());
|
|
99
110
|
this.ws = ws;
|
|
100
111
|
ws.onopen = () => {
|
|
@@ -119,6 +130,13 @@ export class WsClient {
|
|
|
119
130
|
};
|
|
120
131
|
ws.onclose = (event) => {
|
|
121
132
|
this.opts.log?.(`Disconnected (code=${event.code})`);
|
|
133
|
+
// Only handle close for the CURRENT socket. If this.ws has been
|
|
134
|
+
// replaced by a newer connection (or set to null during replacement),
|
|
135
|
+
// this is a stale socket and we must NOT reconnect.
|
|
136
|
+
if (ws !== this.ws) {
|
|
137
|
+
this.opts.log?.("Stale socket closed — ignoring (newer connection exists)");
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
122
140
|
this._clearTimers();
|
|
123
141
|
this.opts.onDisconnect?.();
|
|
124
142
|
if (this.stopped)
|