Commit 638c20886329705347f7103aac5eaae03e31202a
1 parent
f48d284076
Exists in
master
requre require2
Showing
1 changed file
with
1 additions
and
1 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 = requre('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 | ||
| 28 | data.guardarComprobante(message.req).then(() => { | 28 | data.guardarComprobante(message.req).then(() => { | 
| 29 | client.send('ok'); | 29 | client.send('ok'); | 
| 30 | }).catch(err => { | 30 | }).catch(err => { | 
| 31 | client.send('bad'); | 31 | client.send('bad'); | 
| 32 | }); | 32 | }); | 
| 33 | 33 | ||
| 34 | break; | 34 | break; | 
| 35 | 35 | ||
| 36 | default: | 36 | default: | 
| 37 | break; | 37 | break; | 
| 38 | } | 38 | } | 
| 39 | }); | 39 | }); | 
| 40 | }); | 40 | }); | 
| 41 | 41 | ||
| 42 | client.on('error', function(e) { | 42 | client.on('error', function(e) { | 
| 43 | console.log(e); | 43 | console.log(e); | 
| 44 | }) | 44 | }) | 
| 45 | 45 | ||
| 46 | console.log('Sevice connected to wsServer: ' + config.urlHO); | 46 | console.log('Sevice connected to wsServer: ' + config.urlHO); | 
| 47 | 47 |