Socket.IO handling disconnect event

前端 未结 4 622
执念已碎
执念已碎 2020-11-28 21:40

Cant handle this disconnect event, dont know why socket its not send to the client / client doesnt response!

Server

io.sockets.on(\'connection\', fun         


        
4条回答
  •  感动是毒
    2020-11-28 22:14

    Ok, instead of identifying players by name track with sockets through which they have connected. You can have a implementation like

    Server

    var allClients = [];
    io.sockets.on('connection', function(socket) {
       allClients.push(socket);
    
       socket.on('disconnect', function() {
          console.log('Got disconnect!');
    
          var i = allClients.indexOf(socket);
          allClients.splice(i, 1);
       });
    });
    

    Hope this will help you to think in another way

提交回复
热议问题