node.js - 当套接字与服务器断开连接时,如何从房间取消订阅套接字

标签 node.js socket.io sails.js

如果套接字与服务器断开连接,该套接字的所有订阅将默认从聊天室中删除(通过 sails)还是我们必须手动删除(通过代码)

最佳答案

socket.io source code的基础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
};

关于node.js - 当套接字与服务器断开连接时,如何从房间取消订阅套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53118902/

相关文章:

javascript - 未捕获的类型错误 : Object #<Object> has no method 'connect'

javascript - 在共享主机上运行 socket.io

node.js - Azure 虚拟机上的 Sails.js 部署问题

javascript - 用户 session 未在 Node.js 服务器上更新

javascript - 我怎样才能摆脱这个 Promise for 循环?

javascript - 在mongoDB中查询将字符串更改为整数需要很长时间

javascript - Socket.io,广播发射,似乎仅使用最新连接的套接字

javascript - Node.js 无法识别 Coffeescript

javascript - 如何从 Sails/Waterline 的数据库中删除属性

node.js - Nodejs 登录 api null 检查歧义