最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to correctly unsubscribe from a socket.io room and destroy it? - Stack Overflow

programmeradmin3浏览0评论

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)

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)

Share Improve this question edited Aug 29, 2011 at 11:06 Kristaps asked Aug 29, 2011 at 10:09 KristapsKristaps 511 silver badge3 bronze badges 1
  • 5 You should add your solution as an 'answer' so this doesn't e up as 'unanswered' – timoxley Commented Sep 5, 2011 at 5:15
Add a ment  | 

4 Answers 4

Reset to default 7

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

发布评论

评论列表(0)

  1. 暂无评论