socket.io get rooms which socket is currently in

前端 未结 12 1187
臣服心动
臣服心动 2020-12-14 08:40

Is it possible to get rooms which socket is currently in, without calling

io.sockets.clients(roomName)

for every room name and looking for

相关标签:
12条回答
  • 2020-12-14 09:01

    Version 2.0.3

    io.sockets.sockets[yourSocketID].rooms
    

    That equal with

    socket.rooms
    
    0 讨论(0)
  • 2020-12-14 09:01

    socket.io 1.7.3 +

    var currentRoom = socket.rooms[Object.keys(socket.rooms)[0]];//returns name of room
    
    0 讨论(0)
  • 2020-12-14 09:04

    Socket.io v2.1.1

    So make sure you aren't accessing the sockets rooms in the disconnect event like I was, as they have already left the rooms by the time that event is triggered. If you want to do that try it in the disconnecting event - https://github.com/socketio/socket.io/pull/2332/files

    Then you can use any of the following:

    Object.keys(socket.adapter.rooms)
    Object.keys(socket.adapter.sids)
    Object.keys(socket.rooms)
    
    0 讨论(0)
  • 2020-12-14 09:04

    1.4.5 version => io.sockets.adapter.rooms[roomname].sockets

    0 讨论(0)
  • 2020-12-14 09:08

    From the Socket.IO Room doc:

    io.sockets.manager.roomClients[socket.id]

    0 讨论(0)
  • 2020-12-14 09:09

    Being sure that socket is in only one room at a time, my solution was:

    var currentRoom = Object.keys(io.sockets.adapter.sids[socket.id]).filter(item => item!=socket.id);
    
    0 讨论(0)
提交回复
热议问题