socket-io-changes-socket-id-repeatedly part 2

2019-09-10 02:49发布

Following on the question asked by me: socket io changes socket.id repeatedly

Registering only happens when the client comes on the chatpage:

On the client:

       socketConnect: function() {     
       socket =  io.connect('https://socket.mydomain.nl');

       socket.on('connect', function() {
       socket.emit('register', userID, 'Lobby');
       });
       };

Server

    var UserSockIdStorage = {},
         Allsocketids = [];

      //Clients connect and register here. 

        io.sockets.on('connection', function(socket) {             
        socket.on("register", function(userID, currentchannel, userinfostr) {
        console.log('USER WITH ID ' + userID + ' REGISTERS WITH THE SOCKETIDOF: ' + socket.id); 

        UserSockIdStorage[userID] = socket.id; //userid is associated with the socket ID.
        socket.room = currentchannel;
        socket.join(currentchannel);
      });

As you can see i store the username in an array with the socket ID as its value. When a message is to be send the username is looked up to retrieve the socket ID to send the message to.

Now come the part that i don't understand:

Clients keep reconnecting (Not registering so the socket id is not saved in the array!!) from time to time and i can see that they get another socket ID.

My guess would then be, that when i look up by username (Array UserSockIdStorage) a socket ID to send a message to, and the owner of that ID made several reconnects thus acquiring a new socket ID over and over again, that a message send to the lookedUp socket ID wouldn't go anywhere since the client obtained several new socket ID's in the meanwhile.

But from what i'm observing the message is routed to the correct client so the first socket ID is still it's socket ID?!

Are those ID's cached or somewhat?

0条回答
登录 后发表回答