In the same room more than once and customers in the room

I am trying to figure out what happens if clients emit to join the same room more than once. To check and find the answer to this, I wanted to first find out how many rooms customers have after the same customers send more than one to emit to join the room, but the chapter chapter is in the wiki https://github.com/Automattic/socket. io / wiki / Rooms is deprecated. When I try to use "io.sockets.clients (" room ")" I get the error "Object # has no" clients "method.

I had two questions: 1. What happens if a client tries to join the same room more than once? Will he get emissions for this room every time he tries to join? 2. How do I know which clients are in the room?

Im using socket.io v1.0.2

+7
source share
2 answers

I got the answer to this question in socket.io github .

  • According to this line of code, the socket will only receive emissions once. A socket is added to the room only once, and if the same socket is attempted to join the room, this attempt will be ignored.

  • There is currently no open API for receiving clients, and discussion continues in # 1428 . If you really need to get them, for some reason you can get the actual clients from the adapter if you are not using the redis adapter as follows:

    socket.join('test room'); var clients = io.sockets.adapter.rooms['test room']; console.log(clients); for (var clientId in clients) { console.log(io.sockets.connected[clientId]); } 
+15
source share

Fixed receiving clients in the room in socket.io ~ 1.4.5 as follows:

  socket.join('test room'); var room = io.sockets.adapter.rooms['test room']; console.log(room); for (var socketId in room.sockets) { console.log(io.sockets.connected[socketId]); } 
0
source share

All Articles