I'm opening up a tcp connection to a server which is easy enough to do but I need a way to keep that socket open without having to call net.createConnection(port, host) again and again.
What I'm trying to implement is a socket server which accepts multiple connections then channels the requests through the one socket as mentioned above. I then need to channel the response to the correct socket. However, the only issue I'm having is to maintain an open socket which I'm trying to create outside the listening server code.
I've approached it with the Singleton pattern to create the socket..
var Singleton = (function() {
var socket = null;
function connectToHost(port, host) {
socket = net.createConnection(port, host);
return socket;
}
return {
connectToHost: connectToHost
};
})();
But from what I can see, on Event('end') that socket is no longer writable. If I reconnect the socket.
socket.on('end', function() {
socket = Singleton.connectToHost(port, host);
});
the same thing will happen on Event('end').
How can I approach this so that I can create and maintain one socket connection?