【发布时间】:2017-02-13 15:55:02
【问题描述】:
在socket.io 中,是否需要在断开连接触发时手动调用socket.leave()?
还是NodeJs Socket.io 自己处理?
socket.on("disconnect" function() {
this.leave("room"); // is this necessary?
});
【问题讨论】:
在socket.io 中,是否需要在断开连接触发时手动调用socket.leave()?
还是NodeJs Socket.io 自己处理?
socket.on("disconnect" function() {
this.leave("room"); // is this necessary?
});
【问题讨论】:
否,在发出此事件之前,所有房间都已离开。
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
};
【讨论】:
不,当断开事件触发时,您不需要手动调用 socket.leave()。 详情请见http://socket.io/docs/rooms-and-namespaces/
【讨论】: