Add add periodic ping to keep alive
This commit is contained in:
parent
88a7e2f6da
commit
d25c20e2b0
14
index.js
14
index.js
|
@ -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
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue