Commit 892470a118d19ea27f768e50d70fed8a04238fa2

Authored by Eric Fernandez
1 parent bff86f97e7
Exists in master

socket 2

Showing 3 changed files with 28 additions and 12 deletions   Show diff stats
... ... @@ -0,0 +1,11 @@
  1 +module.exports = function(config) {
  2 +
  3 + var knex = require('knex')(config);
  4 +
  5 + return {
  6 + getEntity: function (table, where) {
  7 +
  8 + return knex(table).where(where).select('*');
  9 + }
  10 + }
  11 +}
1 1 const express = require('express');
2 2 const app = express();
  3 +
3 4 const WebSocketClient = require('ws');
4 5 const config = require('./config/config.json');
  6 +const data = require('./data/index')(config.bo);
5 7  
6 8 app.listen(config.port);
  9 +app.use(express.json({limit: '50mb'}));
7 10  
8 11 const client = new WebSocketClient(config.urlHO);
9 12  
10 13 client.on('open', function open() {
11 14 console.log('conection to socket ho is open');
12   -});
13 15  
14   -app.get('/getSocket', function (req, res) {
  16 + client.send(JSON.stringify({ gln: config.gln, action: 'gnl'}));
15 17  
16   - var promesa = new Promise(function (resolve, reject) {
  18 + client.on('message', function incoming(message) {
17 19  
18   - client.send('isAlive?');
19   -
20   - client.on('message', function incoming(message) {
21   - console.log('recibe bo: %s', message);
22   - resolve(message);
23   - });
24   - });
  20 + message = message.data.toString('utf8');
25 21  
26   - promesa.then(function (data) {
27   - res.send(data);
  22 + switch (message) {
  23 + case message.action == 'getEntity':
  24 + data.getEntity(message.tableName, message.where || {}).then(function(data) {
  25 + client.send(data);
  26 + });
  27 + break;
  28 + default:
  29 + break;
  30 + }
28 31 });
29 32 });
30 33  
... ... @@ -10,6 +10,8 @@
10 10 "license": "ISC",
11 11 "dependencies": {
12 12 "express": "^4.16.4",
  13 + "knex": "^0.16.5",
  14 + "mssql": "^5.1.0",
13 15 "ws": "^7.0.0"
14 16 }
15 17 }