I have a small but annoying problem that I can't find the solution for.
On the server side I have this:
...
socket.on('join', function (name)
{
console.log("Joining: "+name);
socket.userName = sanitize(name);
}
socket.on('msg', function(m)
{
console.log(socket.userName+" says: "+m);//This works
});
socket.on('disconnect', function(socket)
{
console.log(socket.userName+" has disconnected");//This does not work
});
...
The issue is that the socket.userName
is available in all my .on
methods exept for socket.on('disconnect',...
I get undefined
for the userName property.
Is this simply not possible to do? If this is not best pratcie, please let me know. I am farily new to node.js
I don't think the
disconnect
event is passed an argument, so you're effectively clobbering yoursocket
variable. Just use an empty argument list for its handler: