I have an internal loop for each socket:
if (!chat.room.list[hash]) { // room has expired
socket.leave(hash);
delete chat.user.list[socket.store.data.id].rooms[hash];
delete socket.store.data.inRooms[hash]; // delete room hash from user store
}
socket.leave(hash)
does nothing - socket still receives messages sent to hash
room.
As a side note - if I connect with client Anna and client Bob - both receive messages, but if I reconnect with client Bob - Bob cannot send messages to Anna.
Is there somewhere a full socket io API documentation (as I couldn't find 开发者_开发百科socket.leave(room) examples)?
EDIT:
Got it! Socket IO saves room handles with slash, so you have to use socket.leave('/'+hash)
Rooms in socket.io are implicitly created and implicitly deleted. Basically they are automatically removed when they are empty.
And yes, a preceding /
is added to the rooms name internally but you do not need to add this to remove a socket from a room.
Try firing console.log(io.sockets.manager.rooms)
whenever a room is created to have a look at what's happening internally.
As of 0.8.7 i tried it out and it seems you don't have to add the / (slash) anymore
socket.leave(hash)
Works just fine.
SocketIO 1.0 has Socket.prototype.leave(roomName)
and Socket.prototype.leaveAll()
.
And you don't have to manually remove rooms:
Upon disconnection, sockets leave all the channels they were part of automatically, and no specially teardown is needed on your part. [From here]
Sort of a tangent, but to answer the part of your question about a full socket.io documentation: Not quite, however, if you check out the source on socket.io's home page you can find some sparse documentation (view the source and CTRL+F for "rooms"). I've had to do this several times already. There's nothing about leaving rooms there, but there is some general explanation.
view-source:http://socket.io
精彩评论