Socket.IO handling disconnect event

前端 未结 4 602
执念已碎
执念已碎 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

    0 讨论(0)
  • 2020-11-28 22:22

    Create a Map or a Set, and using "on connection" event set to it each connected socket, in reverse "once disconnect" event delete that socket from the Map we created earlier

    import * as Server from 'socket.io';
    
    const io = Server();
    io.listen(3000);
    
    const connections = new Set();
    
    io.on('connection', function (s) {
    
      connections.add(s);
    
      s.once('disconnect', function () {
        connections.delete(s);
      });
    
    });
    
    0 讨论(0)
  • 2020-11-28 22:22

    You can also, if you like use socket id to manage your player list like this.

    io.on('connection', function(socket){
      socket.on('disconnect', function() {
        console.log("disconnect")
        for(var i = 0; i < onlineplayers.length; i++ ){
          if(onlineplayers[i].socket === socket.id){
            console.log(onlineplayers[i].code + " just disconnected")
            onlineplayers.splice(i, 1)
          }
        }
        io.emit('players', onlineplayers)
      })
    
      socket.on('lobby_join', function(player) {
        if(player.available === false) return
        var exists = false
        for(var i = 0; i < onlineplayers.length; i++ ){
          if(onlineplayers[i].code === player.code){
            exists = true
          }
        }
        if(exists === false){
          onlineplayers.push({
            code: player.code,
            socket:socket.id
          })
        }
        io.emit('players', onlineplayers)
      })
    
      socket.on('lobby_leave', function(player) {
        var exists = false
        for(var i = 0; i < onlineplayers.length; i++ ){
          if(onlineplayers[i].code === player.code){
            onlineplayers.splice(i, 1)
          }
        }
        io.emit('players', onlineplayers)
      })
    })
    
    0 讨论(0)
  • 2020-11-28 22:23

    For those like @sha1 wondering why the OP's code doesn't work -

    OP's logic for deleting player at server side is in the handler for DelPlayer event, and the code that emits this event (DelPlayer) is in inside disconnected event callback of client.

    The server side code that emits this disconnected event is inside the disconnect event callback which is fired when the socket loses connection. Since the socket already lost connection, disconnected event doesn't reach the client.


    Accepted solution executes the logic on disconnect event at server side, which is fired when the socket disconnects, hence works.

    0 讨论(0)
提交回复
热议问题