Commit c2a1db4112a91bf4268b6cc353d141de998125dc

Authored by Eric Fernandez
1 parent 34f662087e
Exists in master

pin pong

Showing 2 changed files with 28 additions and 3 deletions   Show diff stats
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
9 const client = new WebSocketClient(config.urlHO); 8 const client = new WebSocketClient(config.urlHO);
9 var pingTimeout;
10 var tiempoVivo = new Date();
10 11
11 client.on('open', function open() { 12 client.on('open', function open() {
12 console.log('conection to socket ho is open'); 13 console.log('conection to socket ho is open');
14 tiempoVivo = new Date();
13 15
14 client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); 16 client.send(JSON.stringify({ gln: config.gln, action: 'gln'}));
15 17
16 client.on('message', function incoming(message) { 18 client.on('message', function incoming(message) {
17 19
18 message = JSON.parse(message.toString('utf8')); 20 message = JSON.parse(message.toString('utf8'));
19 21
20 switch (message.action) { 22 switch (message.action) {
21 case 'getEntity': 23 case 'getEntity':
22 24
23 data.getEntity(message).then(function(message) { 25 data.getEntity(message).then(function(message) {
24 26
25 client.send(JSON.stringify(message)); 27 client.send(JSON.stringify(message));
26 }); 28 });
27 break; 29 break;
28 30
29 case 'comprobante': 31 case 'comprobante':
30 console.log('recibiendo comprobante') 32 console.log('recibiendo comprobante')
31 data.guardarComprobante(message.req).then(() => { 33 data.guardarComprobante(message.req).then(() => {
32 console.log('se guardó'); 34 console.log('se guardó');
33 message.ok = 1; 35 message.ok = 1;
34 client.send(JSON.stringify(message)); 36 client.send(JSON.stringify(message));
35 }).catch((ee) => { 37 }).catch((ee) => {
36 console.log('no se guardó' + ee); 38 console.log('no se guardó' + ee);
37 message.ok = 0; 39 message.ok = 0;
38 client.send(JSON.stringify(message)); 40 client.send(JSON.stringify(message));
39 }); 41 });
40 42
41 break; 43 break;
42 44
43 default: 45 default:
44 break; 46 break;
45 } 47 }
46 }); 48 });
49
50 heartbeat();
47 }); 51 });
48 52
49 client.on('error', function(e) { 53 client.on('error', function(e) {
50 console.log(e); 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 console.log('Sevice connected to wsServer: ' + config.urlHO); 77 console.log('Sevice connected to wsServer: ' + config.urlHO);
1 { 1 {
2 "name": "websocketbo", 2 "name": "websocketbo",
3 "version": "0.0.1", 3 "version": "0.0.1",
4 "description": "WebSocket Back Office", 4 "description": "WebSocket Back Office",
5 "main": "index.js", 5 "main": "index.js",
6 "scripts": { 6 "scripts": {
7 "test": "echo \"Error: no test specified\" && exit 1" 7 "test": "echo \"Error: no test specified\" && exit 1"
8 }, 8 },
9 "author": "Foca Software", 9 "author": "Foca Software",
10 "license": "ISC", 10 "license": "ISC",
11 "dependencies": { 11 "dependencies": {
12 "express": "^4.16.4", 12 "express": "^4.16.4",
13 "knex": "^0.16.5", 13 "knex": "^0.16.5",
14 "moment": "^2.24.0",
14 "mssql": "^5.1.0", 15 "mssql": "^5.1.0",
15 "ws": "^7.0.0" 16 "ws": "^7.0.0"
16 } 17 }
17 } 18 }
18 19