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

荒凉一梦 提交于 2020-01-07 03:08:14

问题


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?

来源:https://stackoverflow.com/questions/34334399/socket-io-changes-socket-id-repeatedly-part-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!