【发布时间】:2020-10-01 00:16:17
【问题描述】:
当我打开多个选项卡(1、2、3)时,我在服务器日志中看到套接字包含所有这些特殊套接字。 我还将这个套接字 ID 保存在我的数组中。 当我关闭选项卡 1 时,此 socket.id 从套接字列表中删除。 但是在我的数组中,它只删除了最后打开的套接字。但我需要删除 1。
const array = [];
io.sockets.on('connection', (socket) => {
array.push(socket.id);
//after opened 3 tabs, there be [socketId-1, socketId-2, socketId-3]
//then I closing 1st tab and waiting for result [socketId-2, socketId-3]
socket.on('disconnect', () => {
array = array.filter(() => socketId !== socket.id)
//there will be removed socket.id of last connected tab
console.log(array) // [socketId-1, socketId-2]
})
})
如果我可以定义关闭标签的 socked.id,那么它可以解决我的问题。但是如何在 socket.on('disconnect') 里面定义呢?
【问题讨论】:
-
听起来您想跟踪旧连接。对吗?
-
@SHenry 是的。我需要断开连接的 socket.id,因为我在其他集合中收集它们,它也应该在那里删除。
标签: javascript websocket server socket.io