admin管理员组文章数量:1318573
How do I retrieve the rooms a socket is a member of? I'm using socket.io version 1.4
I tried with this.socket.adapter.rooms
but I received this error in the chrome console: Cannot read property 'rooms' of undefined
In my client code I have this method:
send(msg) {
if(msg != ''){
var clientInfo = [];
clientInfo.push(msg);
clientInfo.push(socket.id);
clientInfo.push(this.socket.adapter.rooms);
socket.emit('message', clientInfo);
}
}
On my server side:
socket.on('message', function(clientInfo){
var clientmessage = clientInfo[0];
var clientid = clientInfo[1];
var clientroom = clientInfo[2];
io.to(clientroom).emit('messageSent', clientmessage);
});
How do I retrieve the rooms a socket is a member of? I'm using socket.io version 1.4
I tried with this.socket.adapter.rooms
but I received this error in the chrome console: Cannot read property 'rooms' of undefined
In my client code I have this method:
send(msg) {
if(msg != ''){
var clientInfo = [];
clientInfo.push(msg);
clientInfo.push(socket.id);
clientInfo.push(this.socket.adapter.rooms);
socket.emit('message', clientInfo);
}
}
On my server side:
socket.on('message', function(clientInfo){
var clientmessage = clientInfo[0];
var clientid = clientInfo[1];
var clientroom = clientInfo[2];
io.to(clientroom).emit('messageSent', clientmessage);
});
Share
Improve this question
edited Aug 12, 2016 at 16:08
splunk
asked Aug 12, 2016 at 12:59
splunksplunk
6,81517 gold badges64 silver badges109 bronze badges
6
-
It's unclear what you're asking.
socket.id
is a unique string for each socket and also corresponds to a room name that only that socket is in. There's also a list of rooms that a given socket is a member of, but I'm not sure what exactly you're looking for? – jfriend00 Commented Aug 12, 2016 at 15:49 - "There's also a list of rooms that a given socket is a member of"... I'm searching for this. – splunk Commented Aug 12, 2016 at 15:55
-
Then, please edit your question to say that's what you're looking for. And, based on your description of the error you are seeing, we need to see a lot more context for your code to understand what
this.socket
is. Also, there is no "single" room name that a socket might belong to as your question implies. A socket can be in many rooms. – jfriend00 Commented Aug 12, 2016 at 15:57 - And, is this code client side code or server side code? – jfriend00 Commented Aug 12, 2016 at 16:02
- The client-side socket does not know what rooms it is in. That is only server-side info. – jfriend00 Commented Aug 12, 2016 at 16:13
2 Answers
Reset to default 5Server side, you can get a list of rooms a socket is in with:
socket.rooms
Client side, a socket does not know what rooms it is in. The whole concept of rooms is a server-side concept and all the data structures are maintained there. If a client wanted to know what rooms it was in, it would either have to keep track of what rooms it requested to be a member of or it would have to ask the server what rooms it is in.
There is one oddity about the server-side socket.rooms
structure. It is apparently not updated real-time. If you do socket.join("someRoom")
and then immediately look at socket.rooms
, you will not see the someRoom
name listed. But, if you look on process.nextTick()
or on setTimeout()
, you will see it in socket.rooms
. I haven't delved into the socket.io source code to figure out why that is the way it is, but apparently something is only being updated asynchronously.
Object.keys(socket.rooms).forEach(function(room, idx) {
if(idx!=0){
console.log(idx,"-->",room)
}
});
By the above code you can identify all the rooms the socket have joined. You can skip the first room which is in index 0 as it is the default room a socket will connect on connection.
Tested this with SocketIO 1.7 and works well.
本文标签: javascriptget room of a socket in socketioStack Overflow
版权声明:本文标题:javascript - get room of a socket in socket.io - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742048540a2417936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论