Commit 892470a118d19ea27f768e50d70fed8a04238fa2
1 parent
bff86f97e7
Exists in
master
socket 2
Showing
3 changed files
with
28 additions
and
12 deletions
Show diff stats
data/index.js
index.js
| 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 |