Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js socketio覆盖onclose方法_Node.js_Sockets - Fatal编程技术网

Node.js socketio覆盖onclose方法

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.rooms。 但是文件说这是不可能的,因为当断开连接触发时,插座已经离开了所有房间

我深入研究了源代码,发现了以下代码:

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