首页 > 解决方案 > 当套接字与服务器断开连接时如何从房间取消订阅套接字

问题描述

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

标签: node.jssocket.iosails.js

解决方案


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
};

推荐阅读