添加用户在线状态更新
This commit is contained in:
@@ -1,28 +1,46 @@
|
|||||||
const connections = new Map();
|
const onlineUsers = new Map();
|
||||||
|
|
||||||
// 保存用户连接
|
// 保存用户连接
|
||||||
function addConnection(username, ws) {
|
function addConnection(username, ws) {
|
||||||
connections.set(username, ws);
|
onlineUsers.set(username, ws);
|
||||||
|
|
||||||
console.log(`用户 ${username} 已连接`);
|
console.log(`用户 ${username} 已连接`);
|
||||||
console.log("当前在线用户:", [...connections.keys()]);
|
console.log("当前在线用户:", [...onlineUsers.keys()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取用户连接
|
// 获取用户连接
|
||||||
function getConnection(username) {
|
function getConnection(username) {
|
||||||
return connections.get(username);
|
return onlineUsers.get(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除用户连接
|
// 删除用户连接
|
||||||
function removeConnection(username) {
|
function removeConnection(username) {
|
||||||
connections.delete(username);
|
onlineUsers.delete(username);
|
||||||
|
|
||||||
console.log(`用户 ${username} 已断开`);
|
console.log(`用户 ${username} 已断开`);
|
||||||
console.log("当前在线用户:", [...connections.keys()]);
|
console.log("当前在线用户:", [...onlineUsers.keys()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取所有在线用户
|
||||||
|
function getOnlineUsers() {
|
||||||
|
return Array.from(onlineUsers.keys());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 广播所有在线用户
|
||||||
|
function broadcast(data) {
|
||||||
|
const message = JSON.stringify(data);
|
||||||
|
|
||||||
|
for (const ws of onlineUsers.values()) {
|
||||||
|
if (ws.readyState === WebSocket.OPEN) {
|
||||||
|
ws.send(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
addConnection,
|
addConnection,
|
||||||
getConnection,
|
getConnection,
|
||||||
removeConnection,
|
removeConnection,
|
||||||
|
getOnlineUsers,
|
||||||
|
broadcast
|
||||||
};
|
};
|
||||||
+14
-1
@@ -8,6 +8,8 @@ const {
|
|||||||
addConnection,
|
addConnection,
|
||||||
getConnection,
|
getConnection,
|
||||||
removeConnection,
|
removeConnection,
|
||||||
|
getOnlineUsers,
|
||||||
|
broadcast
|
||||||
} = require("./connectionManager");
|
} = require("./connectionManager");
|
||||||
|
|
||||||
// 创建WebSocket服务
|
// 创建WebSocket服务
|
||||||
@@ -44,6 +46,12 @@ function initWebSocket(server) {
|
|||||||
// 保存连接
|
// 保存连接
|
||||||
addConnection(username, ws);
|
addConnection(username, ws);
|
||||||
|
|
||||||
|
// 通知所有在线用户当前在线用户列表
|
||||||
|
broadcast({
|
||||||
|
type: "userOnline",
|
||||||
|
userList: getOnlineUsers()
|
||||||
|
});
|
||||||
|
|
||||||
// 接收客户端消息
|
// 接收客户端消息
|
||||||
ws.on("message", (message) => {
|
ws.on("message", (message) => {
|
||||||
try {
|
try {
|
||||||
@@ -59,6 +67,12 @@ function initWebSocket(server) {
|
|||||||
// 连接关闭
|
// 连接关闭
|
||||||
ws.on("close", () => {
|
ws.on("close", () => {
|
||||||
removeConnection(username);
|
removeConnection(username);
|
||||||
|
|
||||||
|
// 通知所有在线用户当前在线用户列表
|
||||||
|
broadcast({
|
||||||
|
type: "userOnline",
|
||||||
|
userList: getOnlineUsers()
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// WebSocket错误
|
// WebSocket错误
|
||||||
@@ -121,7 +135,6 @@ function handleSendMessage(senderName, data) {
|
|||||||
message: "消息保存失败"
|
message: "消息保存失败"
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user