From b20cf372c4fa42d05d697bd7f3ecbf5bd3fc58a4 Mon Sep 17 00:00:00 2001 From: Max Litruv Boonzaayer Date: Fri, 26 Apr 2019 06:55:12 +1000 Subject: [PATCH] more wss stuff --- plugins/wss.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/plugins/wss.js b/plugins/wss.js index 0fd51c3..4e64f69 100644 --- a/plugins/wss.js +++ b/plugins/wss.js @@ -15,6 +15,8 @@ module.exports = { const wss = new WebSocket.Server({ server }); wss.on('connection', function connection(ws) { + ws.isAlive = true; + ws.on('message', function incoming(message) { if (message == "name") ws.send(stringify( @@ -43,6 +45,8 @@ module.exports = { }); + ws.on('pong', heartbeat); + dclient.on('message', (message) => { wss.clients.forEach(function each(client) { if (client !== ws && client.readyState === WebSocket.OPEN) { @@ -54,8 +58,20 @@ module.exports = { // ws.send('something'); }); + function noop() { } + function heartbeat() { + this.isAlive = true; + } + const interval = setInterval(function ping() { + wss.clients.forEach(function each(ws) { + if (ws.isAlive === false) return ws.terminate(); + + ws.isAlive = false; + ws.ping(noop); + }); + }, 5000); server.listen(8080);