简体   繁体   English

socket.io发射后留出空间

[英]socket.io leave room after emitting to it

I would like to know when emit process to a room is done in order to leave room right after. 我想知道何时完成向房间的发射过程以立即离开房间。 Is it OK to leave the room right after emit? 发射后马上离开房间可以吗? I tried using a callback in emit to room but got a server error so I guess it's not possible. 我尝试在房间发射中使用回调,但是遇到服务器错误,所以我猜不可能。 Since I imagine that it's an async process, leaving the room right after emit might not be safe (in a large scale). 既然我想象这是一个异步过程,那么在发出信号后立即离开房间可能不安全(大规模)。 Thoughts? 有什么想法吗?

 //join all friends to a room Socket.join(SOME_ROOM); //send data to this room io.to(SOME_ROOM).emit('SOME_EVENT', SOME_DATA); //cb function fails if I add one //leave room Socket.leave(SOME_ROOM); //is it safe? 

It is safe to leave the room immediately after calling .emit() . 调用.emit()之后立即离开房间是安全的。 The room is only used to figure out which sockets you intend to send to and that is done synchronously in the io.to() call so that is all figured out by the time the .emit() call returns. 该空间仅用于确定要发送给哪个套接字,并在io.to()调用中同步完成,以便在.emit()调用返回时全部确定。

Even if the actual sending of the data to those sockets id done asynchronously (which it is), it no longer matters whether the socket is still in the room or not. 即使将数据实际发送到那些套接字id是异步完成的(事实如此),套接字是否仍在房间中也不再重要。 The room was only used to initially select which sockets to send to and is not used in the actual sending process itself. 该会议室仅用于最初选择要发送到的套接字,而在实际的发送过程中并未使用。

Keep in mind that while a room sounds like an all-powerful concept, all it really is under the covers is a list of sockets. 请记住,虽然一个房间听起来像是一个功能强大的概念,但实际上它背后只是一个插座列表。

So, you should be able to do this just fine: 因此,您应该能够做到这一点:

//join all friends to a room
Socket.join(SOME_ROOM);

//send data to this room
io.to(SOME_ROOM).emit('SOME_EVENT', SOME_DATA);

//leave room
Socket.leave(SOME_ROOM); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM