Note that there are some explanatory texts on larger screens.

plurals
  1. POsocket.io assign userId to sessionId
    text
    copied!<p>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?</p> <p>Here is some sample code..</p> <pre><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'})); }); }); </code></pre> <p>That code works fine and it will show the sessionId when a user disconnects. Here is what I want though</p> <pre><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.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'})); }); }); </code></pre> <p>All I get back is NaN</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload