Socket.IO handling disconnect event

橙三吉。 提交于 2019-11-26 09:19:08

问题


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

Server

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

  socket.on(\'NewPlayer\', function(data1) {

    online = online + 1;
    console.log(\'Online players : \' + online);
    console.log(\'New player connected : \' + data1);
    Players[data1] = data1;
    console.log(Players);

  });

  socket.on(\'DelPlayer\', function(data) {

    delete Players[data];
    console.log(Players);
    console.log(\'Adios\' + data);

  });

  socket.on(\'disconnect\', function () {

      socket.emit(\'disconnected\');
      online = online - 1;

  });

});

Client

 var socket = io.connect(\'http://localhost\');

    socket.on(\'connect\', function () { 

        person_name = prompt(\"Welcome. Please enter your name\");

        socket.emit(\'NewPlayer\', person_name);

        socket.on(\'disconnected\', function() {

            socket.emit(\'DelPlayer\', person_name);

        });

    });

As you can see when a client disconnects the Array object[person_name] should be deleted, but its not


回答1:


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




回答2:


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.




回答3:


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);
  });

});


来源:https://stackoverflow.com/questions/17287330/socket-io-handling-disconnect-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!