V
V
Viacheslav2019-05-06 15:36:19
JavaScript
Viacheslav, 2019-05-06 15:36:19

How to get 2 responses from the server using Promise?

On request from the server, 2 response lines are returned in turn, In the code I get only one!

const net = require("net");

const HOST = '192.168.0.75';
const PORT = '30001';

class Api {

    constructor(host, port) {
        this.HOST = host || HOST;
        this.PORT = port || PORT;
        this.socket = new net.Socket();

        this.init();
    }

    init() {
        const client = this;

        client.socket.connect(client.PORT, client.HOST, () => {
            console.log(`Client connected to: ${client.HOST}:${client.PORT}`);
        });

        client.socket.on('close', () => {
            console.log('FiscalAPI: connection closed');
        });
    };

    sendCommand(cmd) {
        const client = this;

        return new Promise((resolve, reject) => {
            if(cmd) {
                client.socket.write(cmd + '\u0000\r\n');
            }

            client.socket.on('data', (data) => {
                    resolve(data.toString());
            });

            client.socket.on('error', (error) => {
                reject(error);
            });
        });
    };

    checkPumpState(dispenser_index){
        this.sendCommand('0;open')
            .then((data) => {
                console.log(data);
                return this.sendCommand(`0;dispenser_get;${dispenser_index};1`);
            })
            .then((data) => {
                console.log(data);
                return this.sendCommand(`0;store_get;0;16\u0000\r\n`);
            })
            .then((data) => {
                console.log(data);
            })
            .catch((error) => console.log(error));
    }
}

2 responses are returned for each request. First: echo of the request, second: the result needed to continue.
This code only manages to get the first response - echo. How to get the second one too?
PS
Another important point: REQUESTS-RESPONSES must go in strict sequence, the answer to the previous request can be used as data for the next

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2019-05-06
@slaaavyn

I don’t really understand what’s going on here, but I’ll assume that your event client.socket.on('data')is called twice - for each response from the server. And you return the promise as soon as you get the first answer.
UPD accept all data but only return on 'end' event

sendCommand(cmd) {
    if(cmd) {
      this.socket.write(cmd + '\u0000\r\n');
    }
    
      return new Promise((resolve, reject) => {
        const chunks = [];
        
        const handleData = chunk => {
          chunks.push(chunk);
        };

        const handleError = err => {
          reject(err);
        };

        const handleEnd => {
          const data = Buffer.concat(chunks).toString().match(/.*?\r\n/g);
          resolve(data);
        }

        this.socket.on('data', handleData);
        this.socket.on('error', handleError);
        this.socket.on('end', handleEnd);
      });
  }

V
Vitaly, 2019-05-07
@vshvydky

sendCommand creates a subscriber every time it is called, something tells me that your server is running out of memory

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question