From b5bd28fd45a1aca5fdf2614d7d7a971c61838917 Mon Sep 17 00:00:00 2001 From: Eunchong Kim Date: Sun, 8 Aug 2021 14:11:26 +0900 Subject: [PATCH] Added interval function to avoid disconnection --- ws_server.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ws_server.js b/ws_server.js index eb275a3..c327658 100644 --- a/ws_server.js +++ b/ws_server.js @@ -27,7 +27,7 @@ wss.on('connection', (ws) => { // Brige data to all clients ws.onmessage = function(evt) { console.log(evt.data); - wss.clients.forEach((client) => { + wss.clients.forEach( (client) => { client.send(evt.data); }); } @@ -40,9 +40,15 @@ wss.on('connection', (ws) => { }); -// Broadcast updates -//setInterval(() => { -// wss.clients.forEach((client) => { -// client.send(new Date().toTimeString()); -// }); -//}, 1000); +// Send message every 30 s +// Because Heroku terminate connection in 55 s inactivity +// https://devcenter.heroku.com/articles/error-codes#h15-idle-connection +setInterval( () => { + var data = { + ctrl: 'idle', + time: new Date().toTimeString() + } + wss.clients.forEach( (client) => { + client.send( data ); + }); +}, 30000); // 30 s