@wahooks/channel 0.8.0 → 0.8.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.
- package/dist/index.js +19 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -385,8 +385,25 @@ function connectWebSocket() {
|
|
|
385
385
|
const wsUrl = `${wsHost}/api/ws?token=${encodeURIComponent(API_KEY)}`;
|
|
386
386
|
console.error("[wahooks-channel] Connecting to event stream...");
|
|
387
387
|
const ws = new WebSocket(wsUrl);
|
|
388
|
+
let alive = false;
|
|
389
|
+
let heartbeat;
|
|
388
390
|
ws.on("open", () => {
|
|
389
391
|
console.error("[wahooks-channel] Connected to event stream");
|
|
392
|
+
alive = true;
|
|
393
|
+
// Heartbeat: ping every 30s, if no pong within 10s, force reconnect
|
|
394
|
+
heartbeat = setInterval(() => {
|
|
395
|
+
if (!alive) {
|
|
396
|
+
console.error("[wahooks-channel] Heartbeat timeout, reconnecting...");
|
|
397
|
+
clearInterval(heartbeat);
|
|
398
|
+
ws.terminate();
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
alive = false;
|
|
402
|
+
ws.ping();
|
|
403
|
+
}, 30000);
|
|
404
|
+
});
|
|
405
|
+
ws.on("pong", () => {
|
|
406
|
+
alive = true;
|
|
390
407
|
});
|
|
391
408
|
ws.on("message", async (data) => {
|
|
392
409
|
try {
|
|
@@ -477,6 +494,8 @@ function connectWebSocket() {
|
|
|
477
494
|
}
|
|
478
495
|
});
|
|
479
496
|
ws.on("close", (code) => {
|
|
497
|
+
if (heartbeat)
|
|
498
|
+
clearInterval(heartbeat);
|
|
480
499
|
console.error(`[wahooks-channel] Connection closed (${code}), reconnecting in 5s...`);
|
|
481
500
|
setTimeout(connectWebSocket, 5000);
|
|
482
501
|
});
|