简体   繁体   中英

How to check if a socket has been emitted on

I access the socket of a client that connects like this

io.on('connect', (socket) => {
  saveSocketSomwhere(socket)
});

Later on in my code, how can I check if socket has been emitted on? Does it carry any state of that sort?

you have the possibility to save the socket in array like this:

connections = [];
io.sockets.on('connection', function (socket) {
    connections.push(socket);
    console.log('Connected: %s sockets connected', connections.length);

    // and remove like this
    socket.on('disconnect', function (data) {
        connections.splice(connections.indexOf(socket));
        console.log('Disconnected: %s sockets connected', connections.length);
    });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM