Added ping.

This commit is contained in:
ThePendulum 2024-01-13 02:26:03 +01:00
parent c9b985f768
commit 73e60b81f1
1 changed files with 23 additions and 1 deletions

View File

@ -149,7 +149,14 @@ async function connect(bot, games) {
}, },
}); });
socket.ws.on('message', async (msg) => { socket.ws.on('message', async (msgData) => {
const msg = msgData.toString();
if (typeof msg === 'string' && msg.includes('pong')) {
logger.debug(`Received pong ${msg.split(':')[1]}`);
return;
}
const [domain, data] = JSON.parse(msg); const [domain, data] = JSON.parse(msg);
logger.debug(`Received ${domain}: ${JSON.stringify(data)}`); logger.debug(`Received ${domain}: ${JSON.stringify(data)}`);
@ -187,6 +194,21 @@ async function connect(bot, games) {
socket.ws.send(JSON.stringify([domain, data])); socket.ws.send(JSON.stringify([domain, data]));
}; };
function ping() {
setTimeout(() => {
if (socket.ws && socket.ws?.readyState === socket.ws?.OPEN) {
const now = Date.now();
socket.ws.send(`ping:${now}`);
logger.debug(`Sent ping ${now}`);
}
ping();
}, 10000);
}
ping();
socket.connect(); socket.connect();
return socket; return socket;