I have a socket.io server that generates many events, and I want to catch all of them and print a similar message. Right now, I do this:
for (var event in {eventA: 1, eventB: 1, eventC: 1}) {
this.translationSocket.on(event, function(result) {
console.log("Server sent an event of type "+event);
});
}
When the server sends eventA, eventB and eventC, I see this:
Server sent an event of type eventC
Server sent an event of type eventC
Server sent an event of type eventC
i.e. my program catches all events, but always displays the type of eventC...
I tried the following variations:
- remove the "var" before the "event" inside the "for": "for (event ..."
- adding a statement 'var msg = "Server sent an event of type "+event;' either before the 'on' statement or just before the 'console.log' statement, and then 'console.log(msg)'.
None of these variations worked...
What should I do?