Im trying to convert client.sessionId to the value of my variable Response.UserID and all I get is NaN in my response. I have tried several things like toString but still get NaN or [Global object] Has anyone else tried to do this?
Here is some 开发者_如何学Gosample code..
socket.on('connection', function(client){
// Success! Now listen to messages to be received
client.on('message',function(event){
var Response = JSON.parse(event);
if(Response.UserID == null){
client.broadcast(event);
console.log('Received message from client!',event);
}
else{
console.log('Received message from client!',event);
client.broadcast(JSON.stringify({'UserName': '', 'Message': Response.UserID + ' is now connected'}));
}
});
client.on('disconnect',function(){
client.broadcast(JSON.stringify({ 'UserName': '', 'Message':client.sessionId + ' left the conversation'}));
});
});
That code works fine and it will show the sessionId when a user disconnects. Here is what I want though
socket.on('connection', function(client){
// Success! Now listen to messages to be received
client.on('message',function(event){
var Response = JSON.parse(event);
if(Response.UserID == null){
client.broadcast(event);
console.log('Received message from client!',event);
}
else{
console.log('Received message from client!',event);
client.sessionId = Response.UserID;
client.broadcast(JSON.stringify({'UserName': '', 'Message': Response.UserID + ' is now connected'}));
}
});
client.on('disconnect',function(){
client.broadcast(JSON.stringify({ 'UserName': '', 'Message':client.sessionId + ' left the conversation'}));
});
});
All I get back is NaN
Need a little more context here. If Response.UserID is in fact NaN then the first condition of your if statement is returning false and will never execute.
What's the JSON that is being passed as event
to the message? That will answer if UserID is in fact a number or not.
精彩评论