feat: add ping to prevent ws timeout
This commit is contained in:
+12
-2
@@ -32,8 +32,6 @@ function getWhatsApp(): WebSocket | undefined {
|
|||||||
if (ws.readyState === WebSocket.OPEN) {
|
if (ws.readyState === WebSocket.OPEN) {
|
||||||
return ws;
|
return ws;
|
||||||
}
|
}
|
||||||
|
|
||||||
bridgeSockets.delete(ws);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
@@ -134,6 +132,14 @@ wss.on("connection", (ws) => {
|
|||||||
const isBridge =
|
const isBridge =
|
||||||
!url.searchParams.get("client") || url.searchParams.get("bridge") === "1";
|
!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) {
|
if (isBridge) {
|
||||||
bridgeSockets.add(ws);
|
bridgeSockets.add(ws);
|
||||||
console.log("[WS] WhatsApp bridge attached");
|
console.log("[WS] WhatsApp bridge attached");
|
||||||
@@ -143,11 +149,13 @@ wss.on("connection", (ws) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
ws.on("close", () => {
|
ws.on("close", () => {
|
||||||
|
clean();
|
||||||
bridgeSockets.delete(ws);
|
bridgeSockets.delete(ws);
|
||||||
console.log("[WS] WhatsApp bridge disconnected");
|
console.log("[WS] WhatsApp bridge disconnected");
|
||||||
});
|
});
|
||||||
|
|
||||||
ws.on("error", () => {
|
ws.on("error", () => {
|
||||||
|
clean();
|
||||||
bridgeSockets.delete(ws);
|
bridgeSockets.delete(ws);
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
@@ -223,11 +231,13 @@ wss.on("connection", (ws) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
ws.on("close", () => {
|
ws.on("close", () => {
|
||||||
|
clean();
|
||||||
clientSockets.delete(ws);
|
clientSockets.delete(ws);
|
||||||
console.log("[WS] external client disconnected");
|
console.log("[WS] external client disconnected");
|
||||||
});
|
});
|
||||||
|
|
||||||
ws.on("error", () => {
|
ws.on("error", () => {
|
||||||
|
clean();
|
||||||
clientSockets.delete(ws);
|
clientSockets.delete(ws);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user