28 lines
621 B
JavaScript
28 lines
621 B
JavaScript
const connections = new Map();
|
|
|
|
// 保存用户连接
|
|
function addConnection(username, ws) {
|
|
connections.set(username, ws);
|
|
|
|
console.log(`用户 ${username} 已连接`);
|
|
console.log("当前在线用户:", [...connections.keys()]);
|
|
}
|
|
|
|
// 获取用户连接
|
|
function getConnection(username) {
|
|
return connections.get(username);
|
|
}
|
|
|
|
// 删除用户连接
|
|
function removeConnection(username) {
|
|
connections.delete(username);
|
|
|
|
console.log(`用户 ${username} 已断开`);
|
|
console.log("当前在线用户:", [...connections.keys()]);
|
|
}
|
|
|
|
module.exports = {
|
|
addConnection,
|
|
getConnection,
|
|
removeConnection,
|
|
}; |