Socket.io警告:客户端没有握手的客户端应该重新连接

时间:2013-12-23 02:24:32

标签: javascript node.js sockets

以下是我的代码。我一直在几个论坛上讨论如何解决这个问题。我的nodejs断开连接并向我发送此警告消息。 warn: client not handshake client should reconnect

var onInterval = function() {
  countdown--;
  var data = {
      countdown : countdown,
      startTime : startTime,
      endTime : endTime,
 }
 if(countdown == 0){
      var endTime = currTimer();
      winnerBox = randomFromInterval(1,2);
      clearInterval(myInterval);

       setTimeout(function(){

              countdown = 30;
              myInterval = setInterval(onInterval, 1000);
       }, 500);


  }

  io.sockets.emit('timer', { result: data });

}        

var myInterval = setInterval(onInterval, 1000);

io.sockets.on('connection', function (socket){

    socket.on('addUsers', function (data, callback) {

          socket.username = data.username;
          var message;

          if (clients.hasOwnProperty(socket.id)){
              if (clients[socket.id].alias == data.alias && clients[socket.id].itemid ==  data.itemid && clients[socket.id].box == data.box) {
                    delete clients[socket.id];
                    io.sockets.emit('displayUsers', {users: data.alias, cancel : true});

              }else {
                    clients[socket.id].itemid = data.itemid;
                    clients[socket.id].box = data.box;
                    clients[socket.id].key_price = data.key_price;
                    clients[socket.id].alias = data.alias;
                    clients[socket.id].message = '2' ; 
                    clients[socket.id].username = data.username ;  
                    io.sockets.emit('displayUsers', {users: clients[socket.id], cancel : false});
              }



          }else {   
                  clients[socket.id] = {
                      username : data.username, 
                      userid : data.user_id,
                      alias : data.alias,
                      itemid : data.itemid,  
                      box : data.box, 
                      socket_id : socket.id,
                      key_price : data.key_price,
                      message :  '1',
            }

                io.sockets.emit('displayUsers', {users: clients[socket.id], cancel : false});                

          }

    });         

});

1 个答案:

答案 0 :(得分:0)

在客户端使用此代码:

    var socket = io.connect('http://domain.com:port', {
        'connect timeout': 500,
        'reconnect': true,
        'reconnection delay': 500,
        'reopen delay': 500,
        'max reconnection attempts': 10
    });