Commit 0f8c933f54d16056515da3cf740eb5771ab53aba

Authored by Eric Fernandez
1 parent ac5bd61079
Exists in master

cuerpo

Showing 1 changed file with 1 additions and 1 deletions   Show diff stats
webSocketServer/index.js
1 module.exports = function() { 1 module.exports = function() {
2 2
3 const webSocketServer = require('ws').Server; 3 const webSocketServer = require('ws').Server;
4 var clients = []; 4 var clients = [];
5 5
6 const objWs = {}; 6 const objWs = {};
7 7
8 objWs.wsServer = new webSocketServer({ 8 objWs.wsServer = new webSocketServer({
9 port: config.port 9 port: config.port
10 }); 10 });
11 11
12 objWs.wsServer.on('connection', function connection(ws) { 12 objWs.wsServer.on('connection', function connection(ws) {
13 13
14 console.log('open socket server'); 14 console.log('open socket server');
15 15
16 ws.on('message', function incoming(message) { 16 ws.on('message', function incoming(message) {
17 17
18 message = JSON.parse(message.toString('utf8')); 18 message = JSON.parse(message.toString('utf8'));
19 19
20 switch (message.action) { 20 switch (message.action) {
21 case 'gln': 21 case 'gln':
22 clients.push({ws: ws, gln: message.gln}); 22 clients.push({ws: ws, gln: message.gln});
23 break; 23 break;
24 default: 24 default:
25 // console.log(message.action); 25 // console.log(message.action);
26 } 26 }
27 }); 27 });
28 }); 28 });
29 29
30 objWs.getEntidad = function(gln, tableName, where, queryString) { 30 objWs.getEntidad = function(gln, tableName, where, queryString) {
31 31
32 return new Promise(function(resolve, reject) { 32 return new Promise(function(resolve, reject) {
33 33
34 var client = clients.filter(function(client) { 34 var client = clients.filter(function(client) {
35 return client.gln == gln 35 return client.gln == gln
36 })[0]; 36 })[0];
37 37
38 if (!client) { 38 if (!client) {
39 reject('No se encuentra el webSocket client'); 39 reject('No se encuentra el webSocket client');
40 } 40 }
41 41
42 var idSolicitud = Math.round(Math.random() * 1000); 42 var idSolicitud = Math.round(Math.random() * 1000);
43 43
44 var enviar = { 44 var enviar = {
45 action: 'getEntity', 45 action: 'getEntity',
46 idSolicitud: idSolicitud 46 idSolicitud: idSolicitud
47 }; 47 };
48 48
49 if (queryString) { 49 if (queryString) {
50 50
51 enviar.queryString = queryString; 51 enviar.queryString = queryString;
52 } else { 52 } else {
53 enviar.tableName = tableName; 53 enviar.tableName = tableName;
54 enviar.where = where || {}; 54 enviar.where = where || {};
55 } 55 }
56 56
57 client.ws.send(JSON.stringify(enviar)); 57 client.ws.send(JSON.stringify(enviar));
58 58
59 client.ws.on('message', function(message) { 59 client.ws.on('message', function(message) {
60 60
61 message = JSON.parse(message); 61 message = JSON.parse(message);
62 62
63 if (idSolicitud == message.idSolicitud) { 63 if (idSolicitud == message.idSolicitud) {
64 64
65 resolve(message.data); 65 resolve(message.data);
66 } 66 }
67 67
68 }); 68 });
69 }); 69 });
70 } 70 }
71 71
72 objWs.getClientGln = function(gln) { 72 objWs.getClientGln = function(gln) {
73 return clients.filter(function(client) { 73 return clients.filter(function(client) {
74 return client.gln = gln; 74 return client.gln = gln;
75 }); 75 });
76 } 76 }
77 77
78 objWs.guardarComprobante = function (cuerpo, gln) { 78 objWs.guardarComprobante = function (cuerpo, gln) {
79 79
80 return new Promise((resolve, reject) => { 80 return new Promise((resolve, reject) => {
81 81
82 var client = clients.filter(function(client) { 82 var client = clients.filter(function(client) {
83 return client.gln == gln 83 return client.gln == gln
84 })[0]; 84 })[0];
85 85
86 if (!client) { 86 if (!client) {
87 reject('No se encuentra el webSocket client'); 87 reject('No se encuentra el webSocket client');
88 console.log('No se encuentra el webSocket client'); 88 console.log('No se encuentra el webSocket client');
89 return; 89 return;
90 } 90 }
91 91
92 var idSolicitud = Math.round(Math.random() * 1000); 92 var idSolicitud = Math.round(Math.random() * 1000);
93 93
94 var enviar = { 94 var enviar = {
95 action: 'comprobante', 95 action: 'comprobante',
96 req: cuerpo.body 96 req: cuerpo
97 }; 97 };
98 98
99 console.log(`enviando comprobante a cliente`) 99 console.log(`enviando comprobante a cliente`)
100 console.log(client); 100 console.log(client);
101 console.log(cuerpo.body.cabecera); 101 console.log(cuerpo.body.cabecera);
102 102
103 client.ws.send(JSON.stringify(enviar)); 103 client.ws.send(JSON.stringify(enviar));
104 104
105 client.ws.on('message', function(message) { 105 client.ws.on('message', function(message) {
106 106
107 message = JSON.parse(message); 107 message = JSON.parse(message);
108 108
109 if (idSolicitud == message.idSolicitud) { 109 if (idSolicitud == message.idSolicitud) {
110 110
111 resolve(message.data); 111 resolve(message.data);
112 } 112 }
113 113
114 }); 114 });
115 115
116 }); 116 });
117 } 117 }
118 118
119 return objWs; 119 return objWs;
120 } 120 }
121 121