How to unsubscribe a socket from a room when socket disconnected from the server

痞子三分冷 提交于 2019-12-24 00:43:36

问题


If socket is disconnected from the server, Will all the subscription of that socket will be removed from the chat-room by default(by sails) or we have to remove it manually(by code)


回答1:


Base of socket.io source code this.leaveAll() will run before fire disconnect event. So no need to leave from rooms manually

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


来源:https://stackoverflow.com/questions/53118902/how-to-unsubscribe-a-socket-from-a-room-when-socket-disconnected-from-the-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!