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
File was created 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 }
12
1 const express = require('express'); 1 const express = require('express');
2 const app = express(); 2 const app = express();
3
3 const WebSocketClient = require('ws'); 4 const WebSocketClient = require('ws');
4 const config = require('./config/config.json'); 5 const config = require('./config/config.json');
6 const data = require('./data/index')(config.bo);
5 7
6 app.listen(config.port); 8 app.listen(config.port);
9 app.use(express.json({limit: '50mb'}));
7 10
8 const client = new WebSocketClient(config.urlHO); 11 const client = new WebSocketClient(config.urlHO);
9 12
10 client.on('open', function open() { 13 client.on('open', function open() {
11 console.log('conection to socket ho is open'); 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?'); 20 message = message.data.toString('utf8');
19
20 client.on('message', function incoming(message) {
21 console.log('recibe bo: %s', message);
22 resolve(message);
23 });
24 });
25 21
26 promesa.then(function (data) { 22 switch (message) {
27 res.send(data); 23 case message.action == 'getEntity':
24 data.getEntity(message.tableName, message.where || {}).then(function(data) {
25 client.send(data);
26 });
27 break;
28 default:
1 { 1 {
2 "name": "websocketbo", 2 "name": "websocketbo",
3 "version": "0.0.1", 3 "version": "0.0.1",
4 "description": "WebSocket Back Office", 4 "description": "WebSocket Back Office",
5 "main": "index.js", 5 "main": "index.js",
6 "scripts": { 6 "scripts": {
7 "test": "echo \"Error: no test specified\" && exit 1" 7 "test": "echo \"Error: no test specified\" && exit 1"
8 }, 8 },
9 "author": "Foca Software", 9 "author": "Foca Software",
10 "license": "ISC", 10 "license": "ISC",
11 "dependencies": { 11 "dependencies": {
12 "express": "^4.16.4", 12 "express": "^4.16.4",
13 "knex": "^0.16.5",
14 "mssql": "^5.1.0",
13 "ws": "^7.0.0" 15 "ws": "^7.0.0"
14 } 16 }
15 } 17 }
16 18