完善websocket关闭连接功能

This commit is contained in:
2026-09-29 14:13:11 +08:00
parent 9dc511f08b
commit fa7e908a08
3 changed files with 8 additions and 4 deletions
+6 -2
View File
@@ -16,9 +16,13 @@ function getConnection(username) {
}
// 删除用户连接
function removeConnection(username) {
onlineUsers.delete(username);
function removeConnection(username, ws) {
const currentWs = onlineUsers.get(username);
// 只有 Map 里面存的还是这个连接,才允许删除
if (currentWs === ws) {
onlineUsers.delete(username);
}
console.log(`用户 ${username} 已断开`);
console.log("当前在线用户:", [...onlineUsers.keys()]);
}