在socket.io
中,断开连接时是否需要手动拨打socket.leave()
?
或NodeJs Socket.io
本身处理它?</ p>
socket.on("disconnect" function() {
this.leave("room"); // is this necessary?
});
答案 0 :(得分:12)
否,所有房间在发出此事件之前已经离开。
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/