Wednesday, 24 May 2017

change callback of socket.on("data", fn) nodejs

Problem

I have a socket variable that connects to a host and a port:

let socket;
socket = net.createConnection( connectOpts, () => {
    socket.on( "data", () => console.log("Hello World"));
} );

However, later on, I want to change the listener function. To achieve this I do:

socket.on( "data", () => console.log("Hello MARS"));

The problem is that when mu socket gets data, I see both Hello World and Hello MARS printed !

Question

How do I change the listener function from Hello World to Hello MARS?



via Flame_Phoenix

No comments:

Post a Comment