Commit 52b838b421ecc55fc1c778e1d1edbe395d56c854
1 parent
d9d4c20d43
Exists in
master
ping pong
Showing
1 changed file
with
17 additions
and
0 deletions
Show diff stats
webSocketServer/index.js
| ... | ... | @@ -9,9 +9,17 @@ module.exports = function() { |
| 9 | 9 | port: config.port |
| 10 | 10 | }); |
| 11 | 11 | |
| 12 | + function noop() {} | |
| 13 | + | |
| 14 | + function heartbeat() { | |
| 15 | + this.isAlive = true; | |
| 16 | + } | |
| 17 | + | |
| 12 | 18 | objWs.wsServer.on('connection', function connection(ws) { |
| 13 | 19 | |
| 14 | 20 | console.log('open socket server'); |
| 21 | + ws.isAlive = true; | |
| 22 | + ws.on('pong', heartbeat); | |
| 15 | 23 | |
| 16 | 24 | ws.on('message', function incoming(message) { |
| 17 | 25 | |
| ... | ... | @@ -27,6 +35,15 @@ module.exports = function() { |
| 27 | 35 | }); |
| 28 | 36 | }); |
| 29 | 37 | |
| 38 | + const interval = setInterval(function ping() { | |
| 39 | + objWs.wsServer.clients.forEach(function each(ws) { | |
| 40 | + if (ws.isAlive === false) return ws.terminate(); | |
| 41 | + | |
| 42 | + ws.isAlive = false; | |
| 43 | + ws.ping(noop); | |
| 44 | + }); | |
| 45 | + }, 10000); | |
| 46 | + | |
| 30 | 47 | objWs.getEntidad = function(gln, tableName, where, queryString) { |
| 31 | 48 | |
| 32 | 49 | return new Promise(function(resolve, reject) { |