Add add periodic ping to keep alive

This commit is contained in:
Christopher Cookman 2025-06-03 14:25:46 -06:00
parent 88a7e2f6da
commit d25c20e2b0
2 changed files with 11 additions and 3 deletions

0
README.md Normal file
View file

View file

@ -23,15 +23,20 @@ const wsMain = () => {
headers: {
"User-Agent": "DiscordHookProxyClient v1.0",
"Client-ID": process.env.CLIENT_ID,
},
}
});
let pingTimer;
wsClient.on("open", () => {
console.log("WebSocket connection established".green);
// Send periodic pings to keep the connection alive
pingTimer = setInterval(() => {
if (wsClient.readyState === ws.OPEN) {
wsClient.send("ping");
}
}, 15000 + (Math.random() * 2 - 1).toFixed(2) * 1000); // Send a ping every 30 seconds
});
wsClient.on("message", (rawdata) => {
console.log(rawdata)
let message
try {
message = JSON.parse(rawdata);
@ -65,6 +70,9 @@ const wsMain = () => {
});
wsClient.on("close", (code, reason) => {
if (pingTimer) {
clearInterval(pingTimer);
}
console.log(`WebSocket connection closed (${code} ${reason}). Reconnecting...`.yellow);
setTimeout(wsMain, 5000); // Reconnect after 5 seconds
});