Reputation: 17610
I need to read some of a stream's (TCP/net.Socket
) data before knowing where it should be piped. But the data doesn't seem to be reaching the endSocket
.
socket.on('data', data => {
var endSocket = getEndSocket(data);
stream
.pipe(endSocket)
.pipe(socket); // send response back
});
The following code does work, but I have a feeling I should be using .pipe()
for simplicity.
socket.on('data', data => {
var endSocket = getEndSocket(data);
endSocket.write(data);
endSocket.pipe(socket); // send response back
});
Upvotes: 0
Views: 31
Reputation: 6998
It depends what you want to do with the rest of the data. If you add a pipe to socket and keep the data listener and you send more packages over the same socket every package will be replicated to all endSocket
's that are being piped to.
The pattern that suits your first example is if you want to have an initial packet which selects what endSocket to connect to. Then you should set up a two-way pipe, and disconnect the data listener on that packet. You also have to write the data that you already read, as pipe will not replay that to the endSocket.
If every package that comes in from the same sourcesocket should go to different endSockets your second example looks perfect.
Upvotes: 1