socket.io assigning custom socket.id

后端 未结 2 1219
别那么骄傲
别那么骄傲 2021-02-02 03:53

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

2条回答
  •  梦毁少年i
    2021-02-02 04:06

    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

提交回复
热议问题