From c2a1db4112a91bf4268b6cc353d141de998125dc Mon Sep 17 00:00:00 2001 From: Eric Fernandez Date: Fri, 23 Aug 2019 14:51:46 -0300 Subject: [PATCH] pin pong --- index.js | 30 +++++++++++++++++++++++++++--- package.json | 1 + 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 694f2ac..f919112 100644 --- a/index.js +++ b/index.js @@ -5,11 +5,13 @@ module.exports = knex = require('knex')(config.bo), moment = require('moment'); - const client = new WebSocketClient(config.urlHO); +var pingTimeout; +var tiempoVivo = new Date(); client.on('open', function open() { console.log('conection to socket ho is open'); + tiempoVivo = new Date(); client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); @@ -19,7 +21,7 @@ client.on('open', function open() { switch (message.action) { case 'getEntity': - + data.getEntity(message).then(function(message) { client.send(JSON.stringify(message)); @@ -44,10 +46,32 @@ client.on('open', function open() { break; } }); + + heartbeat(); }); client.on('error', function(e) { console.log(e); -}) +}); + +client.on('ping', heartbeat); + +client.on('close', function clear() { + clearTimeout(pingTimeout); +}); + +function heartbeat() { + console.log('recibiendo ping'); + console.log(`tiempo vivo desde ${tiempoVivo}`); + + clearTimeout(pingTimeout); + // Use `WebSocket#terminate()`, which immediately destroys the connection, + // instead of `WebSocket#close()`, which waits for the close timer. + // Delay should be equal to the interval at which your server + // sends out pings plus a conservative assumption of the latency. + pingTimeout = setTimeout(() => { + client.terminate(); + }, 10000 + 1000); +} console.log('Sevice connected to wsServer: ' + config.urlHO); diff --git a/package.json b/package.json index e0789e2..28ece45 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "express": "^4.16.4", "knex": "^0.16.5", + "moment": "^2.24.0", "mssql": "^5.1.0", "ws": "^7.0.0" } -- 1.9.1