Socket.IO在浏览器关闭时删除所有连接

时间:2014-02-07 10:38:51

标签: javascript node.js javascript-events socket.io

服务器端代码:

//When a user disconnects
  socket.on("disconnect", function(){
    console.log(rooms.length);
    var destroyThis = null;

    if(typeof socket.store.data.roomi == 'undefined'){
      for(var i in rooms){
        if(rooms[i].roomSocket.id == socket.id){
          destroyThis = rooms[i];
        }
      }
      if(destroyThis !== null){rooms.splice(destroyThis, 1);}
      console.log(rooms.length);
    }else{
      var roomId = socket.store.data.roomi;
      for(var i in rooms[roomId].mobileSockets){
        if(rooms[roomId].mobileSockets[i] == socket){
          destroyThis = i;
        }
      }
      if(destroyThis !== null){rooms[roomId].mobileSockets.splice(destroyThis, 1);}
      rooms[roomId].roomSocket.emit('remove user', socket.id);
    }
  });

    var express =require('express');
    var app = express()
        , http = require('http')
        , server = http.createServer(app)
        , io = require('socket.io').listen(server);

    server.listen(8080);

    app.configure(function(){
        app.use(express.methodOverride());
        app.use(express.bodyParser());
        app.use(app.router);
    });

    app.configure('development', function(){
        app.use(express.static(__dirname + '/public'));
        app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));

    });

    app.configure('production', function(){
      var oneYear = 31557600000;
      app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
      app.use(express.errorHandler());

    });

客户端代码:

socket.on('remove user', function(socketID){
      var destroyThis = null;
      for(var i in players){
        if(players[i].id == socketID){
          destroyThis = i;
        }
      }
      if(destroyThis !== null){ players.splice(destroyThis, 1);}

  });

当我尝试在移动平台上没有连接时关闭一个浏览器选项卡时,它会关闭所有活动连接。我该怎么做才能避免这种行为?

编辑:我试过socket.IO的v0.99和v1.0。这是调试信息

info - 传输结束(套接字结束)

debug - 设置客户端HTAVoq8f22uRDvSrHxT2的关闭超时

debug - 清除客户端HTAVoq8f22uRDvSrHxT2的关闭超时

debug - 清除客户端HTAVoq8f22uRDvSrHxT2的心跳间隔

2

1

调试 - 丢弃传输

debug - 为客户端发送心跳Fqli8YOR_cjF-3PeHxT0

debug - websocket writing 2 ::

debug - 为客户端设置心跳超时Fqli8YOR_cjF-3PeHxT0

debug - 获取心跳包

debug - 清除客户端Fqli8YOR_cjF-3PeHxT0的心跳超时

debug - 为客户端设置心跳间隔Fqli8YOR_cjF-3PeHxT0

0 个答案:

没有答案