0

My website has an IM with several users connected. From my client I wish to disconnect a particular user. Here is the code I am trying:

  // client side
function deleteUser(delCallsign)
     {
          delCallsign = delCallsign.toUpperCase();
            socket.emit('deleteuser', delCallsign); // send it to the server for delete
     }

// server side
socket.on('deleteuser', function(callsign)
   {
        socket.disconnect(usernames[callsign]);
      io.sockets.emit('updateusers', usernames);
    });

Using an alert, I have verified that I'm calling the server side function with the username I wish to disconnect. But what happens is that I get disconnected, not the user specified. What am I doing wrong here?

K4elo
  • 73
  • 1
  • 5
  • Does the array contains the socket.id of the individual you are trying to disconnect? Also this might be relevent http://stackoverflow.com/questions/25976501/socketio-disconnect-client-by-socket-id – Musk Apr 23 '15 at 17:14
  • Sorry, forgot to say - the usernames array hold the user names. – K4elo Apr 23 '15 at 17:30
  • but is there something down the line that pass the socket.id of the user to be disconnected... socket.disconnect() needs a socket.id parameter otherwise it will fall to its default behavior which is close the sockets. – Musk Apr 23 '15 at 17:39
  • Well, no and that explains why it is disconnecting me instead of the desired user. When a user is connected they are stored in the socket session with socket.username = username; and to the array with usernames[username] = username; So how would I go about getting the socket.id of the user I want to disconnect? – K4elo Apr 23 '15 at 17:45

1 Answers1

0

On user connection you should record its socket.id which you would then call for deletion

io.sockets.on('connection',function(socket){
      // Asign socket.id to variable 
      // socket.id;
});

socket.on('deleteuser', function(callsign) {
      io.sockets.connected[usernames[callsign].id].disconnect();
      io.sockets.emit('updateusers', usernames);  
});

This is roughly the idea.

Based on those post:

SocketIO: disconnect client by socket id?

Get the client id of the message sender in socket.io?

-- More Relevent --

A little bit old but the same principals applies

how do I store socket resources from specific users with socket.io?

Community
  • 1
  • 1
Musk
  • 1,477
  • 1
  • 15
  • 25