node.js - 当套接字与服务器断开连接时如何从房间取消订阅套接字
问题描述
如果套接字与服务器断开连接,该套接字的所有订阅是否会默认从聊天室中删除(通过sails)或者我们必须手动删除它(通过代码)
解决方案
socket.io 源代码 的基础this.leaveAll()
将在触发断开事件之前运行。所以无需手动离开房间
Socket.prototype.onclose = function(reason){
if (!this.connected) return this;
debug('closing socket - reason %s', reason);
this.emit('disconnecting', reason);
this.leaveAll(); // leave from all rooms
this.nsp.remove(this);
this.client.remove(this);
this.connected = false;
this.disconnected = true;
delete this.nsp.connected[this.id];
this.emit('disconnect', reason); // disconnect event fire here
};
推荐阅读
- javascript - 当键值为空时,从对象数组中删除嵌套对象
- python - TF 2.6:“AssignVariableOp”Op 的输入“资源”Op 的 float32 类型与预期的资源类型不匹配
- javascript - 从 javascript 参考案例中重现 python 中的 hmac 操作
- node.js - HTTP 请求 - 请求模块 - 包含 html 转义字符的 302 重定向位置
- apache-camel - 在 Spring Camel 文件组件中设置自定义 processStrategy 会抛出找不到合适的属性设置器:processStrategy
- categorical-data - brms:我如何设置具有多个分类变量的模型,以便所有级别都存在并且没有纳入一般截距?
- r - 如何对小标题和小标题中的列/值运行操作,小标题在列表中?
- sql - 查询功能 Google Sheet - 聚合 +(不需要的)排序
- laravel - 查询生成器 when() 方法产生条件错误的查询
- java - 无法使用 tomcat、servlet 和 spring 加载我的网页