socket.io get rooms which socket is currently in

前端 未结 12 1192
臣服心动
臣服心动 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 08:45

    When using a non-default adapter, such as socket.io-redis, socket.rooms doesn't seem to do the trick. The way I managed to get the rooms for a specific client without looping was to use io.sockets.adapter.sids[socket.id], which returns the rooms as an object.

    { 'R-ZRgSf7h4wfPatcAAAC': true, ROOM: true, ROOM_2: true }
    

    Note that this doesn't list sockets on other processes, though!

    socket.io v1.3.7, socket.io-redis 1.0.0

提交回复
热议问题