[英]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?稍后在我的代码中,如何检查是否已发出
socket
? Does it carry any state of that sort?它是否带有任何此类 state?
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);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.