Commit 4c627571a5f6a8e72b2bdedd3984ddd2b361faac
Exists in
master
Merge branch 'master' into 'master'
Master See merge request !10
Showing
1 changed file
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 | console.log(message); | |
| 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 | console.log('recibiendo comprobante') | 27 | console.log('recibiendo comprobante') |
| 28 | data.guardarComprobante(message.req).then(() => { | 28 | data.guardarComprobante(message.req).then(() => { |
| 29 | console.log('se guardó'); | 29 | console.log('se guardó'); |
| 30 | client.send(JSON.stringify('ok')); | 30 | client.send(JSON.stringify('ok')); |
| 31 | }).catch(() => { | 31 | }).catch(() => { |
| 32 | console.log('no se guardó'); | 32 | console.log('no se guardó'); |
| 33 | client.send(JSON.stringify('bad')); | 33 | client.send(JSON.stringify('bad')); |
| 34 | }); | 34 | }); |
| 35 | 35 | ||
| 36 | break; | 36 | break; |
| 37 | 37 | ||
| 38 | default: | 38 | default: |
| 39 | break; | 39 | break; |
| 40 | } | 40 | } |
| 41 | }); | 41 | }); |
| 42 | }); | 42 | }); |
| 43 | 43 | ||
| 44 | client.on('error', function(e) { | 44 | client.on('error', function(e) { |
| 45 | console.log(e); | 45 | console.log(e); |
| 46 | }) | 46 | }) |
| 47 | 47 | ||
| 48 | console.log('Sevice connected to wsServer: ' + config.urlHO); | 48 | console.log('Sevice connected to wsServer: ' + config.urlHO); |
| 49 | 49 |