2012-03-05 22 views
1

私はNode.jsとSocket.ioでチャットアプリケーションを作成していますが、接続が切断されると残りのユーザーは相手が切断されたという警告を受け取ります。Socket.io再接続するか完全に切断するかを検出する

問題は、たまにはSocket.ioが自動的に接続を解除して再接続することです。パートナーが実際に切断していないにもかかわらず、私のチャットアプリケーションがアラートをトリガーしています。

コード:

var clients = {}; 
var soloClients = []; 
io.sockets.on('connection', function (socket) { 

    //Session start 
    socket.on('sessionStart', function() { 
     clients[socket.id] = socket; 
     soloClients.push(socket.id); 

     var searchClients = function(){ 
      if(soloClients.length > 1){ 
       var rand = Math.floor(Math.random() * soloClients.length); 
       if(soloClients[rand] && soloClients[rand] != socket.id){ 
        if(clients[soloClients[rand]]){ 
         var you = clients[socket.id]; 
         var partner = clients[soloClients[rand]] 

         clients[partner.id]['partner'] = you.id; 
         clients[you.id]['partner'] = partner.id; 
         soloClients.splice(soloClients.indexOf(you.id), 1); 
         soloClients.splice(soloClients.indexOf(partner.id), 1); 

         partner.emit('partnerConnect', null); 
         socket.emit('partnerConnect', null); 
        } 
        else{ 
         soloClients.splice(rand, 1); 
         searchClients; 
        } 
       } 
       else{ 
        searchClients(); 
       } 
      } 
     }; 
     searchClients(); 
    }); 

    //On disconnect 
    socket.on('disconnect', function(){ 
     soloClients.splice(soloClients.indexOf(socket.id), 1); 

     if(clients[socket.id]){ 
      if(clients[clients[socket.id]['partner']]){ 
       clients[clients[socket.id]['partner']].emit('partnerDisconnect', null); 
      } 
      delete clients[socket.id]; 
     } 
    }); 
}); 

これを解決するためにどのような方法があれば、私は思っていました。

ありがとうございます!

答えて

0

この動作は起こりそうにないことが判明しました。それはそのバージョンの単なるバグでした。このバグは最新バージョンで修正されています。

0

おそらく、クライアントが切断される本当の理由を調べるべきですか?私。それは悪い接続、またはファイアウォールの問題、または何か他のですか?

また、通常の通知を行うクリーンな切断(エラーなし)の場合は、接続解除のエラーをチェックする必要がありますが、エラーの場合は別の方法で処理する必要があります。

関連する問題