完善websocket关闭连接功能
This commit is contained in:
@@ -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()]);
|
||||
}
|
||||
|
||||
+1
-1
@@ -68,7 +68,7 @@ function initWebSocket(server) {
|
||||
|
||||
// 连接关闭
|
||||
ws.on("close", () => {
|
||||
removeConnection(username);
|
||||
removeConnection(username, ws);
|
||||
|
||||
// 通知所有在线用户当前在线用户列表
|
||||
broadcast({
|
||||
|
||||
Reference in New Issue
Block a user