socket.io: disconnect event isn't fired

后端 未结 3 2031
遥遥无期
遥遥无期 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:28

    Just in case anyone else made this silly mistake: make sure that any socket middleware you've defined calls next() at the end, or else no other socket handlers will run.

    // make sure to call next() at the end or...
    io.use(function (socket, next) {
        console.log(socket.id, "connection middleware");
        next(); // don't forget this!
    });
    
    // ...none of the following will run:
    
    io.use(function (socket, next) {
        console.log(socket.id, "second middleware");
        next(); // don't forget this either!
    });
    
    io.on("connection", function (socket) {
        console.log(socket.id, "connection event");
        socket.once("disconnect", function () {
            console.log(socket.id, "disconnected");
        });
    });
    

提交回复
热议问题