Saturday 23 April 2016

How to use the net's socket object outside of its callback function?

First of all, I'm not even sure I"m asking this correctly, but here's what I want to do. I'll start with a "sketch" of the code:var net = require('net'); someObject.prototype.changeState= function(callback){ // I want to use the server to send some data when this object method is called callback(); } var server = net.createServer(function(socket) { socket.write("Echo test server! \n"); socket.on('data', function(chunk){ socket.write(chuck); console.log("Received: " + chunk); }); socket.on('end', socket.end); }); What I need to do is have the server permanently on once I start the node script, but when I call the changState method, I want to send some data on the socket. The only way I know that it works right know is creating the server object each time I call the changeState method, but that is not an elegant solution. Any recommendations? :)

Submitted April 23, 2016 at 03:42PM by MrZeratul

No comments:

Post a Comment