socket.io,使用网络套接字的聊天应用程序 - 关闭 tab1 导致 tab2 和 tab3 无效 "online users"
socket.io, chat app using web sockets- closing tab1 results in void "online users" in tab2 and tab3
在我使用网络套接字的聊天应用程序中,只要有超过 1 个用户连接(比如 {user1: tab1, user2: tab2, user2: tab3, ...}),在关闭 tab1 时,"online users" 其他选项卡(tab2、tab3、...)中的状态变为空。
然而,在做相反的事情时,即关闭除 tab1 之外的选项卡时,我的聊天应用程序通过实时更新 "online users" 其他选项卡中的状态。
我不知道我的代码的哪一部分出了问题,所以我提供了指向 index.html 和 [=24= 的所有编码部分的链接]
index.html
server.js
我用的nodejs版本10.15.0,express版本6.7.0,socket.io版本6.7.0.
我希望这么多信息足够了。
我认为错误在这里:
users.splice(users.indexOf(socket.username))
来自 Array.splice documentation :
array.splice(start[, deleteCount])
If deleteCount is omitted [...] then all of the elements from start
through the end of the array will be deleted.
因此,当第一个用户断开连接时,您将他们 和所有后续用户 从数组中删除。
我猜你应该这样写:
users.splice(users.indexOf(socket.username), 1)
一次只删除一个用户。
在我使用网络套接字的聊天应用程序中,只要有超过 1 个用户连接(比如 {user1: tab1, user2: tab2, user2: tab3, ...}),在关闭 tab1 时,"online users" 其他选项卡(tab2、tab3、...)中的状态变为空。
然而,在做相反的事情时,即关闭除 tab1 之外的选项卡时,我的聊天应用程序通过实时更新 "online users" 其他选项卡中的状态。
我不知道我的代码的哪一部分出了问题,所以我提供了指向 index.html 和 [=24= 的所有编码部分的链接]
index.html
server.js
我用的nodejs版本10.15.0,express版本6.7.0,socket.io版本6.7.0.
我希望这么多信息足够了。
我认为错误在这里:
users.splice(users.indexOf(socket.username))
来自 Array.splice documentation :
array.splice(start[, deleteCount])
If deleteCount is omitted [...] then all of the elements from start through the end of the array will be deleted.
因此,当第一个用户断开连接时,您将他们 和所有后续用户 从数组中删除。
我猜你应该这样写:
users.splice(users.indexOf(socket.username), 1)
一次只删除一个用户。