Commit 532685f3390a29a43662b830e1d7252f28e5bf2e

Authored by Eric
1 parent 179ad7a7fa
Exists in master

socket, config

Showing 2 changed files with 49 additions and 0 deletions   Show diff stats
config/config.ejemplo.json
... ... @@ -0,0 +1,4 @@
  1 +{
  2 + "port": 6544,
  3 + "direccion": "fipdebo.ddns.net"
  4 +}
... ... @@ -0,0 +1,45 @@
  1 +const socket = {};
  2 +const Net = require('net');
  3 +
  4 +socket.initConection = function(port, host) {
  5 +
  6 + socket.client = Net.Socket();
  7 +
  8 + return new Promise(function(resolve, reject) {
  9 +
  10 + socket.client.connect({ port: port, host: host }, function () {
  11 +
  12 + resolve();
  13 + });
  14 + });
  15 +};
  16 +
  17 +socket.getTank = function(code) {
  18 +
  19 + //caracter necesario
  20 + var soh = String.fromCharCode(01);
  21 + var respuesta = '';
  22 +
  23 + socket.client.write(soh + code);
  24 +
  25 +
  26 + return new Promise(function(resolve, reject) {
  27 +
  28 + socket.client.on('data', function(chunk) {
  29 +
  30 + var data = chunk.toString('utf8');
  31 +
  32 + respuesta += data;
  33 +
  34 + if (respuesta[respuesta.length - 1].charCodeAt(0) == 3) {
  35 + resolve(respuesta);
  36 + }
  37 + });
  38 + });
  39 +};
  40 +
  41 +socket.end = function() {
  42 + socket.client.end();
  43 +};
  44 +
  45 +module.exports = socket;