Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  169] [ 7]  / answers: 1 / hits: 29413  / 11 Years ago, thu, may 23, 2013, 12:00:00

I have a socket id of a connection. Can I get the status of that connection, inside the function handler of another one?



Something like this:



io.sockets.on('connection', function(socket) {
/* having the socket id of *another* connection, I can
* check its status here.
*/
io.sockets[other_socket_id].status
}


Is there a way to do so?


More From » node.js

 Answers
38

For versions higher than 1.0, check Karan Kapoor answer.
For older versions, you can access any connected socket with io.sockets.sockets[a_socket_id], so if you've set a status variable on it, io.sockets.sockets[a_socket_id].status will work.



First you should check if the socket really exists, and it can also be used to check connected/disconnected statuses.



if(io.sockets.sockets[a_socket_id]!=undefined){
console.log(io.sockets.sockets[a_socket_id]);
}else{
console.log(Socket not connected);
}

[#78062] Wednesday, May 22, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminkyrap

Total Points: 631
Total Questions: 89
Total Answers: 109

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
;