I am quite new to nodeJS and Socket.IO and am facing a problem.
Is there any way to know the type of event inside a Socket.IO handler? I have this kind of code:
// Fire the appropriate callback when we receive a message
// that.received is an array of callback functions
for (var event in that.received) {
socket.on(event, function(message) {
// Of course, this won't work because of "event" scope
that.received[event](message, this);
});
}
So, what I want to know is the actual value of “event” that triggered my handler.
I tried to inspect available variables with Chrome developer tools, but I wasn’t able to find anything.
I need to do this because I am writing some kind of wrapper class around Socket.IO to handle multiple sockets (a long storty about fallback servers). I want it to be generic enough to pass my handlers to it.
Any ideas?
OK, stupid question.
I just need to do this:
I was passing “this” to my callback function, which is just stupid. My callbacks looked like this:
But all I have to do is this:
So I don’t have the scope problem anymore.