简体   繁体   English

如何收听房间事件

[英]How to listen for Room events

So to clarify, I know the client should send a message such as 'join-room' to the server for handling, and thats fine, and I know I can do the same for 'leave-room'. 为了明确起见,我知道客户端应该向服务器发送诸如“ join-room”之类的消息以进行处理,这很好,并且我知道我可以对“ leave-room”执行相同的操作。

What I want is to actually listen to the events themself, because say if a client disconnects, I need to send a message to the rooms affected, as when a client disconnects then they automatically leave all rooms. 我想要的是实际监听事件本身,因为如果客户断开连接,我需要向受影响的房间发送消息,因为当客户断开连接时,他们会自动离开所有房间。

So I want something like; 所以我想要类似的东西;

socket.on('join' function() {
// send message
});

socket.on('leave' function() {
// send message
});

So that if the user closes the window (which disconnects the client, and then triggers all the rooms to be left) I can send a message out. 这样,如果用户关闭窗口(这将断开客户端的连接,然后触发所有房间离开),我可以发送一条消息。

I am using the latest socket.io, with the redis adaptor. 我正在使用最新的socket.io和redis适配器。

Also; 也;

What is the most efficient way to list all the rooms a particular socket is in, as technically I think the disconnect event should contain the client_id so I could do this manually. 列出特定套接字所在的所有房间的最有效方法是什么,因为从技术上讲,我认为断开事件应包含client_id,因此我可以手动执行此操作。

The key thing is clarity and stability, although I am open to alternate approaches (must still use socket.io). 关键是清晰度和稳定性,尽管我愿意接受其他方法(必须仍然使用socket.io)。

Thanks 谢谢

So if I understood correctly your main question: you want to listen to the events of joining and leaving a room in the implementation itself such that socket.join(room_id) should fire that event you want to listen to. 因此,如果我正确理解了您的主要问题:您想监听实现本身中加入和离开房间的事件,以使socket.join(room_id)应该触发您想监听的事件。

The thing is the docs dont mention any ways of listening to that event. 问题是文档没有提及监听该事件的任何方法。 In fact, the implementation itself shows no sign of firing an event when joining a room. 实际上, 实现本身未显示加入会议室时触发事件的迹象。 My guess is that you will have to implement the join_room/leave_room system to handle that. 我的猜测是,您将必须实现join_room/leave_room系统来进行处理。

As for the sub-question, the socket object contains an array called rooms such that socket.rooms contains all the rooms the socket is currently in. 至于子问题,套接字对象包含一个称为rooms的数组,因此socket.rooms包含套接字当前位于的所有房间。

This is probably what you're looking for : 这可能是您要寻找的:

socket.on('disconnect', function() { for(var room in socket.rooms) { //do stuff before the client close the connection and leave the rooms } });

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

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