Socket.IO handling disconnect event

前端 未结 4 621
执念已碎
执念已碎 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: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);
      });
    
    });
    

提交回复
热议问题