I've this code:
for (i in namespaces)
{
namespaces[i].on('connection',handleConnection(namespaces[i]));
function handleConnection(ns)
{
return function (socket){//connection
socket.on('setUsername',setUsernameCallback);//event received
function setUsernameCallback(userN){//I want to move this
socket.username = userN;
//some code
}
};
}
}
In order to make my code easier to read, I want to make setUsernameCallback
function defined in an outer scope or maybe in another JS file, like that:
for (i in namespaces)
{
namespaces[i].on('connection',handleConnection(namespaces[i]));
function handleConnection(ns)
{
return function (socket){//connection
socket.on('setUsername',setUsernameCallback);//event received
};
}
}
function setUsernameCallback(userN){
socket.username = userN;
//some code
}
Actually, this is not working. I tried different ways to pass parameter but I'm still getting undefined socket.