index.js 1.18 KB
const WebSocketClient = require('ws');
const config = require('./config/config.json');
const data = require('./data/index')(config.bo);
module.exports = knex = require('knex')(config.bo);

const client = new WebSocketClient(config.urlHO);

client.on('open', function open() {
  console.log('conection to socket ho is open');

  client.send(JSON.stringify({ gln: config.gln, action: 'gln'}));

  client.on('message', function incoming(message) {

    message = JSON.parse(message.toString('utf8'));

    switch (message.action) {
      case 'getEntity':

        data.getEntity(message).then(function(message) {

          client.send(JSON.stringify(message));
        });
        break;

      case 'comprobante':
        console.log('recibiendo comprobante')
        data.guardarComprobante(message.req).then(() => {
          console.log('se guardó');
          client.send(JSON.stringify('ok'));
        }).catch(() => {
          console.log('no se guardó');
          client.send(JSON.stringify('bad'));
        });

        break;

      default:
        break;
    }
  });
});

client.on('error', function(e) {
  console.log(e);
})

console.log('Sevice connected to wsServer: ' + config.urlHO);