Commit 34f662087e1077aa1349a158113470480d60878e

Authored by Eric Fernandez
1 parent 51d936da8b
Exists in master

number

Showing 1 changed file with 2 additions and 2 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 8
9 const client = new WebSocketClient(config.urlHO); 9 const client = new WebSocketClient(config.urlHO);
10 10
11 client.on('open', function open() { 11 client.on('open', function open() {
12 console.log('conection to socket ho is open'); 12 console.log('conection to socket ho is open');
13 13
14 client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); 14 client.send(JSON.stringify({ gln: config.gln, action: 'gln'}));
15 15
16 client.on('message', function incoming(message) { 16 client.on('message', function incoming(message) {
17 17
18 message = JSON.parse(message.toString('utf8')); 18 message = JSON.parse(message.toString('utf8'));
19 19
20 switch (message.action) { 20 switch (message.action) {
21 case 'getEntity': 21 case 'getEntity':
22 22
23 data.getEntity(message).then(function(message) { 23 data.getEntity(message).then(function(message) {
24 24
25 client.send(JSON.stringify(message)); 25 client.send(JSON.stringify(message));
26 }); 26 });
27 break; 27 break;
28 28
29 case 'comprobante': 29 case 'comprobante':
30 console.log('recibiendo comprobante') 30 console.log('recibiendo comprobante')
31 data.guardarComprobante(message.req).then(() => { 31 data.guardarComprobante(message.req).then(() => {
32 console.log('se guardó'); 32 console.log('se guardó');
33 message.ok = true; 33 message.ok = 1;
34 client.send(JSON.stringify(message)); 34 client.send(JSON.stringify(message));
35 }).catch((ee) => { 35 }).catch((ee) => {
36 console.log('no se guardó' + ee); 36 console.log('no se guardó' + ee);
37 message.ok = false; 37 message.ok = 0;
38 client.send(JSON.stringify(message)); 38 client.send(JSON.stringify(message));
39 }); 39 });
40 40
41 break; 41 break;
42 42
43 default: 43 default:
44 break; 44 break;
45 } 45 }
46 }); 46 });
47 }); 47 });
48 48
49 client.on('error', function(e) { 49 client.on('error', function(e) {
50 console.log(e); 50 console.log(e);
51 }) 51 })
52 52
53 console.log('Sevice connected to wsServer: ' + config.urlHO); 53 console.log('Sevice connected to wsServer: ' + config.urlHO);
54 54