I'm looking for a method or a command line to get the socket room name if possible. Any ideas or tips are highly appreciated! For example if the name of the room is 'roomName', I'm looking to get this value, ty!
相关问题
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- google-drive can't get push notifications
- How to reimport module with ES6 import
- Why is `node.js` dying when called from inside pyt
- How to verify laravel passport api token in node /
相关文章
- node连接远程oracle报错
- How can make folder with Firebase Cloud Functions
- @angular-cli install fails with deprecated request
- node.js modify file data stream?
- How to resolve hostname to an ip address in node j
- Transactionally writing files in Node.js
- Log to node console or debug during webpack build
- Get file created date in node
A socket.io socket can be in multiple rooms. In fact, it is automatically placed into a room with the same name as the
socket.id
value when the socket first connects.If what you're trying to do is get a list of all the rooms a socket is in on the server-side of the connection, you can use
socket.rooms
. That is an object that lists all the rooms the socket is in. If you want just an array of room names the socket is in, you can use this:If you want to eliminate the auto-generated room that matches the
socket.id
, you can do this:If you're trying to get this information from the client-end of things, it is not available from the client-side socket. You would have to ask the server which rooms this client is in or you'd have to have a system where the client was kept notified of what rooms it was being put in and it kept track of that data. The socket.io client does not know what rooms it is in.
One other thing to be careful of in socket.io. I've seen some asynchronous behavior in joining rooms which means that if you
socket.join("someRoom")
and then immediately querysocket.rooms
, the new room might not be there yet. If you querysocket.rooms
on the next tick, it will be there. I'm not sure if this always happens or just happens in some circumstances.