V
V
Vladimir Krauz2016-08-07 23:31:36
JavaScript
Vladimir Krauz, 2016-08-07 23:31:36

NodeJs How to get message from 'net' library not via event?

Good evening.

I use the "net" library to communicate between the client and the nodejs server.
It makes it possible to call the send function at any time anywhere (example: socket.write(data)).
So, now I need the reverse side of this task, I need to get the data not through the event (socket.on("data")), but directly:
var buffer = socket.read() (this is an example of the desired result).

I have two types of sockets in my program: regular TCP connections and WebSocket(socket.io). So, a request comes to socket.io with data that I must send to the client on TCP, but also immediately wait for a response (the answer will be fast, because I need to literally ping the client to check its busyness). Since the sockets are different, the listeners are also different, so it is very inconvenient to receive data from someone else's listener, because of this I want to use socket.read()

How can this be implemented? It is possible an example of the code, it is possible an article, it is possible to describe simply words.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
yeti357, 2016-08-11
@MelancholicTheDie

Create a buffer into which to dump the data from socket.on('data'). And then, if necessary, read from it and clean it.

So, a request comes to socket.io with data that I have to send to the client on TCP,
here it is better to organize pipe. I don't know about socket.io, but the standard socket from net is a stream.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question