Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  16] [ 6]  / answers: 1 / hits: 29333  / 8 Years ago, thu, march 24, 2016, 12:00:00

I keep track the list of every users connected in the array.
So if there is a new connection, it will check whether the user is already on the list or not, if he was already on the list, then assign their socket.id with the corresponding socket.id on the list, otherwise just add them to the list.



It's for preventing same user counted as 2 user while he attempt to do multi-login.



Object.keys(client).forEach(function (key) {
if (client[key].id == data.id){
is_connected = true;
socket.id = key;
}
});


I have no problem handling the messages/chat that was sent/received by the user who attempt multi-login.



socket.on('chat', function(msg){
var data = {name: client[socket.id].name, message: msg};
io.emit('chat', data);
});


The io.emit for the chat message was succesfully sent to the user who attempting multi-login.



The problem I got was whenever the user decide to logout/disconnect from the server.



io.emit('user_leave', client[socket.id].id);


[Multi-Login Case] -> Multi-User and Dual-User are same user attempting Multi-Login



Whenever the Main-User disconnected from the server, the Dual-User received 'user_leave' sent by the server, because io.emit supposed to send it to all sockets.



But not otherwise, while the Sub-User disconnected from the server, the Main-user do not receive 'user_leave' emitted by the server.



*Note: Main-User is login first, then the Dual-User. So the Main-User information was saved directly in the array, while the Sub-User socket.id was assigned with the Main-User socket.id



[Update]



Explanation



B2 socket.id was assigned with B1 socket.id, the io.emit for chat work perfectly while io.emit for disconnect only emitted to All except Dual-User(B2)


More From » node.js

 Answers
9

socket.id is used internally by socket.io for its own socket list. You cannot overwrite that or you break some of its ability to maintain its own data structures.



You have two choices:




  1. You can use the existing socket.id value as is (without overwriting it) so you don't break existing behavior. It is already guaranteed to be unique on the server.

  2. You can use a different property name for your own id such as socket.userId and then you won't conflict.



If you need to, you can maintain a map between your own custom id and the socket.io socket.id so you could get to one from the other.



Similar question here: Socket.io custom client ID


[#62816] Tuesday, March 22, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianom

Total Points: 601
Total Questions: 98
Total Answers: 109

Location: Kenya
Member since Fri, Dec 23, 2022
1 Year ago
lucianom questions
Tue, Feb 22, 22, 00:00, 2 Years ago
Wed, May 5, 21, 00:00, 3 Years ago
Sun, Jan 24, 21, 00:00, 3 Years ago
Sat, Aug 15, 20, 00:00, 4 Years ago
Mon, Jun 22, 20, 00:00, 4 Years ago
;