它是否需要在Nodejs中断开连接的socket.leave()

时间:2017-02-13 07:46:46

标签: node.js socket.io

socket.io中,断开连接时是否需要手动拨打socket.leave()? 或NodeJs Socket.io本身处理它?<​​/ p>

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

2 个答案:

答案 0 :(得分:12)

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

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

答案 1 :(得分:3)

当断开连接事件时,你不需要手动调用socket.leave()。 有关详细信息,请参阅http://socket.io/docs/rooms-and-namespaces/