【问题标题】:Does it need to socket.leave() a room on disconnect in Nodejs是否需要在 Nodejs 中断开连接时 socket.leave() 一个房间
【发布时间】:2017-02-13 15:55:02
【问题描述】:

socket.io 中,是否需要在断开连接触发时手动调用socket.leave()
还是NodeJs Socket.io 自己处理?

socket.on("disconnect" function() {
   this.leave("room"); // is this necessary?
});

【问题讨论】:

    标签: node.js socket.io


    【解决方案1】:

    ,在发出此事件之前,所有房间都已离开。

    Code from socket.io:

    Socket.prototype.onclose = function(reason){
      if (!this.connected) return this;
      debug('closing socket - reason %s', reason);
      this.emit('disconnecting', reason);
      this.leaveAll();   //leaving 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);   //emitting event
    };
    

    【讨论】:

      【解决方案2】:

      不,当断开事件触发时,您不需要手动调用 socket.leave()。 详情请见http://socket.io/docs/rooms-and-namespaces/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-09
        • 2016-04-09
        • 1970-01-01
        • 1970-01-01
        • 2016-02-24
        • 2011-05-26
        相关资源
        最近更新 更多