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