Y
Y
Yaroslav2017-02-24 21:06:03
Node.js
Yaroslav, 2017-02-24 21:06:03

How to wrap a tcp socket connection between two servers in a promise?

There is an authorization and session storage server.
To simplify to a minimum:

const net = require('net');
const server = net.createServer((socket) => {
   socket.on('data', (data) => {
      //обрабатываем данные
      socket.write(data); //отправляем обработанные
      });
   socket.on('end', () => {});
});
server.listen(1333, () => {
  console.log('server bound');
});

And there are microprocesses that access the server for information:
const net = require('net');
const client = new net.Socket();
client.setEncoding('utf8');

client.connect('1333','0.0.0.1', ()=>{});
client.write(JSON.stringify({ param : data.toString()}));
client.on('data', (data) => {
   //обрабатываем полученные от сервера результаты
});

The question is, is it necessary to wrap the request of each client in a promise? And if so, how...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
emp1re, 2017-02-25
@emp1re

It can also be a regular callback. The main thing is that your logic is executed asynchronously.

V
Vitaly, 2017-02-25
@vshvydky

It’s definitely not in the promise, since the promise works out and dies, and the sockets are still stream events, and not one-time

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question