Node.js socketio覆盖onclose方法
当用户断开连接时,需要向其所有房间广播。因此,我需要在服务器端断开连接事件中获取socket.rooms。 但是文件说这是不可能的,因为当断开连接触发时,插座已经离开了所有房间 我深入研究了源代码,发现了以下代码:Node.js socketio覆盖onclose方法,node.js,sockets,Node.js,Sockets,当用户断开连接时,需要向其所有房间广播。因此,我需要在服务器端断开连接事件中获取socket.rooms。 但是文件说这是不可能的,因为当断开连接触发时,插座已经离开了所有房间 我深入研究了源代码,发现了以下代码: Socket.prototype.onclose = function(reason){ if (!this.connected) return this; debug('closing socket - reason %s', reason); this.leaveAl
Socket.prototype.onclose = function(reason){
if (!this.connected) return this;
debug('closing socket - reason %s', reason);
this.leaveAll();
this.nsp.remove(this);
this.client.remove(this);
this.connected = false;
this.disconnected = true;
delete this.nsp.connected[this.id];
this.emit('disconnect', reason);
};
也许我应该重写此方法并插入断开连接事件,如下所示:
io.on("connection", function(socket){
var onclose = socket.onclose;
socket.onclose = function(reason){
this.emit('disconnecting', reason);
onclose.apply(socket, reason);
};
//other logic ...
}
但这段代码不起作用。
需要一些建议或其他解决方案。TKS
附言:
节点版本:v0.12.3
socket.io版本:1.3.2不是一种聪明的方法,但最终它可以工作
io.on("connection", function(socket){
var onclose = socket.onclose;
socket.onclose = function(reason){
this.__disconnecting(reason);
onclose.call(socket, reason);
};
socket.__disconnecting = function(reason){
console.log(socket.rooms);
};
// other logic ...
}