当套接字与服务器断开连接时如何从房间取消订阅套接字 [英] How to unsubscribe a socket from a room when socket disconnected from the server

查看:48
本文介绍了当套接字与服务器断开连接时如何从房间取消订阅套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

推荐答案

Base of socket.io 源代码 this.leaveAll() 将在触发断开事件之前运行.所以无需手动离开房间

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

这篇关于当套接字与服务器断开连接时如何从房间取消订阅套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆