feat: add ping to prevent ws timeout

This commit is contained in:
2026-09-15 18:26:35 +08:00
parent 501ee73e41
commit d0edccf15d
+12 -2
View File
@@ -32,8 +32,6 @@ function getWhatsApp(): WebSocket | undefined {
if (ws.readyState === WebSocket.OPEN) {
return ws;
}
bridgeSockets.delete(ws);
}
return undefined;
@@ -134,6 +132,14 @@ wss.on("connection", (ws) => {
const isBridge =
!url.searchParams.get("client") || url.searchParams.get("bridge") === "1";
const ping = setInterval(() => {
ws.send(JSON.stringify({ type: "ping" }));
}, 10_000);
const clean = () => {
clearInterval(ping);
};
if (isBridge) {
bridgeSockets.add(ws);
console.log("[WS] WhatsApp bridge attached");
@@ -143,11 +149,13 @@ wss.on("connection", (ws) => {
});
ws.on("close", () => {
clean();
bridgeSockets.delete(ws);
console.log("[WS] WhatsApp bridge disconnected");
});
ws.on("error", () => {
clean();
bridgeSockets.delete(ws);
});
return;
@@ -223,11 +231,13 @@ wss.on("connection", (ws) => {
});
ws.on("close", () => {
clean();
clientSockets.delete(ws);
console.log("[WS] external client disconnected");
});
ws.on("error", () => {
clean();
clientSockets.delete(ws);
});
});