fix: improved ping-pong events

This commit is contained in:
Lukian 2025-04-09 11:51:28 +02:00
parent 1fee32f1d2
commit 3ecac00706
2 changed files with 8 additions and 1 deletions

View file

@ -33,6 +33,13 @@ router.ws('/', function(ws, req) {
ws.on('close', () => {
sockets.delete(ws);
});
ws.on('message', (msg) => {
const data = JSON.parse(msg);
if (data.type === 'ping') {
ws.send(JSON.stringify({ type: 'pong' }));
}
});
});
function socketsMiddleware(req, res, next) {

View file

@ -12,7 +12,7 @@ import ChannelsPage from './pages/ChannelsPage'
const socket = new WebSocket("/api/ws");
setInterval(() => {
socket.send("ping");
socket.send(JSON.stringify({ type: "ping" }));
}, 30 * 1000);
createRoot(document.getElementById('root')!).render(