socket.io: disconnect event isn't fired

后端 未结 3 2110
遥遥无期
遥遥无期 2021-02-01 02:36

I have made a simple realtime visitor counter.

You can download it from this repository.

What happens is that disconnect event (even after browser closing) on se

3条回答
  •  广开言路
    2021-02-01 03:24

    Put your on disconnect code inside your on connect block and edit it a bit like so:

    io.sockets.on('connection', function (socket) {
        count++;
        io.sockets.emit('count', {
            number: count
        });
    
        socket.on('disconnect', function () {
            console.log('DISCONNESSO!!! ');
            count--;
            io.sockets.emit('count', {
                number: count
            });
        });
    });
    

    This way you're detecting when a specific socket (specifically the socket you pass to your anonymous function that is run on connection) is disconnected.

提交回复
热议问题