generated from lucien/api-template
fix: improved ping-pong events
This commit is contained in:
parent
1fee32f1d2
commit
3ecac00706
2 changed files with 8 additions and 1 deletions
|
@ -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) {
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue