Commit c2a1db4112a91bf4268b6cc353d141de998125dc
1 parent
34f662087e
Exists in
master
pin pong
Showing
2 changed files
with
28 additions
and
3 deletions
Show diff stats
index.js
... | ... | @@ -5,11 +5,13 @@ module.exports = |
5 | 5 | knex = require('knex')(config.bo), |
6 | 6 | moment = require('moment'); |
7 | 7 | |
8 | - | |
9 | 8 | const client = new WebSocketClient(config.urlHO); |
9 | +var pingTimeout; | |
10 | +var tiempoVivo = new Date(); | |
10 | 11 | |
11 | 12 | client.on('open', function open() { |
12 | 13 | console.log('conection to socket ho is open'); |
14 | + tiempoVivo = new Date(); | |
13 | 15 | |
14 | 16 | client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); |
15 | 17 | |
... | ... | @@ -19,7 +21,7 @@ client.on('open', function open() { |
19 | 21 | |
20 | 22 | switch (message.action) { |
21 | 23 | case 'getEntity': |
22 | - | |
24 | + | |
23 | 25 | data.getEntity(message).then(function(message) { |
24 | 26 | |
25 | 27 | client.send(JSON.stringify(message)); |
... | ... | @@ -44,10 +46,32 @@ client.on('open', function open() { |
44 | 46 | break; |
45 | 47 | } |
46 | 48 | }); |
49 | + | |
50 | + heartbeat(); | |
47 | 51 | }); |
48 | 52 | |
49 | 53 | client.on('error', function(e) { |
50 | 54 | console.log(e); |
51 | -}) | |
55 | +}); | |
56 | + | |
57 | +client.on('ping', heartbeat); | |
58 | + | |
59 | +client.on('close', function clear() { | |
60 | + clearTimeout(pingTimeout); | |
61 | +}); | |
62 | + | |
63 | +function heartbeat() { | |
64 | + console.log('recibiendo ping'); | |
65 | + console.log(`tiempo vivo desde ${tiempoVivo}`); | |
66 | + | |
67 | + clearTimeout(pingTimeout); | |
52 | 68 | |
69 | + // Use `WebSocket#terminate()`, which immediately destroys the connection, | |
70 | + // instead of `WebSocket#close()`, which waits for the close timer. | |
71 | + // Delay should be equal to the interval at which your server | |
72 | + // sends out pings plus a conservative assumption of the latency. | |
73 | + pingTimeout = setTimeout(() => { | |
74 | + client.terminate(); | |
75 | + }, 10000 + 1000); | |
76 | +} | |
53 | 77 | console.log('Sevice connected to wsServer: ' + config.urlHO); |