Commit b98e2730fff3cae6f8fcc1ee7338d45583236dcc
1 parent
892470a118
Exists in
master
avances
Showing
1 changed file
with
9 additions
and
5 deletions
 
Show diff stats
index.js
| 1 | const express = require('express'); | 1 | const express = require('express'); | 
| 2 | const app = express(); | 2 | const app = express(); | 
| 3 | 3 | ||
| 4 | const WebSocketClient = require('ws'); | 4 | const WebSocketClient = require('ws'); | 
| 5 | const config = require('./config/config.json'); | 5 | const config = require('./config/config.json'); | 
| 6 | const data = require('./data/index')(config.bo); | 6 | const data = require('./data/index')(config.bo); | 
| 7 | 7 | ||
| 8 | app.listen(config.port); | 8 | app.listen(config.port); | 
| 9 | app.use(express.json({limit: '50mb'})); | 9 | app.use(express.json({limit: '50mb'})); | 
| 10 | 10 | ||
| 11 | const client = new WebSocketClient(config.urlHO); | 11 | const client = new WebSocketClient(config.urlHO); | 
| 12 | 12 | ||
| 13 | client.on('open', function open() { | 13 | client.on('open', function open() { | 
| 14 | console.log('conection to socket ho is open'); | 14 | console.log('conection to socket ho is open'); | 
| 15 | 15 | ||
| 16 | client.send(JSON.stringify({ gln: config.gln, action: 'gnl'})); | 16 | client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); | 
| 17 | 17 | ||
| 18 | client.on('message', function incoming(message) { | 18 | client.on('message', function incoming(message) { | 
| 19 | 19 | ||
| 20 | message = message.data.toString('utf8'); | 20 | message = JSON.parse(message.toString('utf8')); | 
| 21 | |||
| 22 | switch (message.action) { | ||
| 23 | case 'getEntity': | ||
| 21 | 24 | ||
| 22 | switch (message) { | ||
| 23 | case message.action == 'getEntity': | ||
| 24 | data.getEntity(message.tableName, message.where || {}).then(function(data) { | 25 | data.getEntity(message.tableName, message.where || {}).then(function(data) { | 
| 25 | client.send(data); | 26 | |
| 27 | message.data = data[0]; | ||
| 28 | |||
| 29 | client.send(JSON.stringify(message)); | ||
| 26 | }); | 30 | }); | 
| 27 | break; | 31 | break; | 
| 28 | default: | 32 | default: | 
| 29 | break; | 33 | break; | 
| 30 | } | 34 | } | 
| 31 | }); | 35 | }); | 
| 32 | }); | 36 | }); | 
| 33 | 37 |