【问题标题】:How to unsubscribe a socket from a room when socket disconnected from the server当套接字与服务器断开连接时如何从房间取消订阅套接字
【发布时间】:2019-04-06 17:13:28
【问题描述】:

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

【问题讨论】:

    标签: node.js socket.io sails.js


    【解决方案1】:

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

    【讨论】:

    • 谢谢@Milad。是的,所有连接的套接字都会在客户端断开连接时从房间中删除。我还使用 Object.keys(sails.io.sockets.connected) 进行了验证 - 它只会将连接的套接字返回到服务器。
    猜你喜欢
    • 2016-03-09
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多