Commit 8bea3a9dc468fb6f87b47c601adf894c21bd1381
1 parent
e528eadf15
Exists in
master
process exit
Showing
1 changed file
with
1 additions
and
0 deletions
Show diff stats
index.js
| 1 | const WebSocketClient = require('ws'); | 1 | const WebSocketClient = require('ws'); |
| 2 | const config = require('./config/config.json'); | 2 | const config = require('./config/config.json'); |
| 3 | const data = require('./data/index')(config.bo); | 3 | const data = require('./data/index')(config.bo); |
| 4 | module.exports = | 4 | module.exports = |
| 5 | knex = require('knex')(config.bo), | 5 | knex = require('knex')(config.bo), |
| 6 | moment = require('moment'); | 6 | moment = require('moment'); |
| 7 | 7 | ||
| 8 | const client = new WebSocketClient(config.urlHO); | 8 | const client = new WebSocketClient(config.urlHO); |
| 9 | var pingTimeout; | 9 | var pingTimeout; |
| 10 | var tiempoVivo = new Date(); | 10 | var tiempoVivo = new Date(); |
| 11 | 11 | ||
| 12 | client.on('open', function open() { | 12 | client.on('open', function open() { |
| 13 | console.log('conection to socket ho is open'); | 13 | console.log('conection to socket ho is open'); |
| 14 | tiempoVivo = new Date(); | 14 | tiempoVivo = new Date(); |
| 15 | 15 | ||
| 16 | client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); | 16 | client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); |
| 17 | 17 | ||
| 18 | client.on('message', function incoming(message) { | 18 | client.on('message', function incoming(message) { |
| 19 | 19 | ||
| 20 | message = JSON.parse(message.toString('utf8')); | 20 | message = JSON.parse(message.toString('utf8')); |
| 21 | 21 | ||
| 22 | switch (message.action) { | 22 | switch (message.action) { |
| 23 | case 'getEntity': | 23 | case 'getEntity': |
| 24 | 24 | ||
| 25 | data.getEntity(message).then(function(message) { | 25 | data.getEntity(message).then(function(message) { |
| 26 | 26 | ||
| 27 | client.send(JSON.stringify(message)); | 27 | client.send(JSON.stringify(message)); |
| 28 | }); | 28 | }); |
| 29 | break; | 29 | break; |
| 30 | 30 | ||
| 31 | case 'comprobante': | 31 | case 'comprobante': |
| 32 | console.log('recibiendo comprobante') | 32 | console.log('recibiendo comprobante') |
| 33 | data.guardarComprobante(message.req).then(() => { | 33 | data.guardarComprobante(message.req).then(() => { |
| 34 | console.log('se guardó'); | 34 | console.log('se guardó'); |
| 35 | message.ok = 1; | 35 | message.ok = 1; |
| 36 | client.send(JSON.stringify(message)); | 36 | client.send(JSON.stringify(message)); |
| 37 | }).catch((ee) => { | 37 | }).catch((ee) => { |
| 38 | console.log('no se guardó' + ee); | 38 | console.log('no se guardó' + ee); |
| 39 | message.ok = 0; | 39 | message.ok = 0; |
| 40 | client.send(JSON.stringify(message)); | 40 | client.send(JSON.stringify(message)); |
| 41 | }); | 41 | }); |
| 42 | 42 | ||
| 43 | break; | 43 | break; |
| 44 | 44 | ||
| 45 | default: | 45 | default: |
| 46 | break; | 46 | break; |
| 47 | } | 47 | } |
| 48 | }); | 48 | }); |
| 49 | 49 | ||
| 50 | heartbeat(); | 50 | heartbeat(); |
| 51 | }); | 51 | }); |
| 52 | 52 | ||
| 53 | client.on('error', function(e) { | 53 | client.on('error', function(e) { |
| 54 | console.log(e); | 54 | console.log(e); |
| 55 | }); | 55 | }); |
| 56 | 56 | ||
| 57 | client.on('ping', heartbeat); | 57 | client.on('ping', heartbeat); |
| 58 | 58 | ||
| 59 | client.on('close', function clear() { | 59 | client.on('close', function clear() { |
| 60 | clearTimeout(pingTimeout); | 60 | clearTimeout(pingTimeout); |
| 61 | }); | 61 | }); |
| 62 | 62 | ||
| 63 | function heartbeat() { | 63 | function heartbeat() { |
| 64 | console.log('recibiendo ping'); | 64 | console.log('recibiendo ping'); |
| 65 | console.log(`tiempo vivo desde ${tiempoVivo}, ${new Date()}`); | 65 | console.log(`tiempo vivo desde ${tiempoVivo}, ${new Date()}`); |
| 66 | 66 | ||
| 67 | clearTimeout(pingTimeout); | 67 | clearTimeout(pingTimeout); |
| 68 | 68 | ||
| 69 | // Use `WebSocket#terminate()`, which immediately destroys the connection, | 69 | // Use `WebSocket#terminate()`, which immediately destroys the connection, |
| 70 | // instead of `WebSocket#close()`, which waits for the close timer. | 70 | // instead of `WebSocket#close()`, which waits for the close timer. |
| 71 | // Delay should be equal to the interval at which your server | 71 | // Delay should be equal to the interval at which your server |
| 72 | // sends out pings plus a conservative assumption of the latency. | 72 | // sends out pings plus a conservative assumption of the latency. |
| 73 | pingTimeout = setTimeout(() => { | 73 | pingTimeout = setTimeout(() => { |
| 74 | client.terminate(); | 74 | client.terminate(); |
| 75 | process.exit(1); | ||
| 75 | }, 10000 + 1000); | 76 | }, 10000 + 1000); |
| 76 | } | 77 | } |
| 77 | console.log('Sevice connected to wsServer: ' + config.urlHO); | 78 | console.log('Sevice connected to wsServer: ' + config.urlHO); |
| 78 | 79 |