修复用户退出提示bug

This commit is contained in:
shuaiplus committed 2025-05-10 04:20:24 +08:00
1 parent 4ff9751b45
commit 6fe9c708df
1 file changed
+16 -20
+16 -20
View File
@@ -84,23 +84,17 @@ function createUserItem(user, isMe) {
function handleClientList(idx, list, selfId) {
const rd = roomsData[idx];
if (!rd) return;
// 记录旧用户ID集合
const oldUserIds = new Set((rd.userList || []).map(u => u.clientId));
// 新用户ID集合
const newUserIds = new Set(list.map(u => u.clientId));
// 检查退出用户(旧有,新没有)
if (rd.isInitialized) {
for (const oldId of oldUserIds) {
if (!newUserIds.has(oldId)) {
const user = rd.userMap[oldId];
const name = user ? (user.userName || user.username || user.name || 'Anonymous') : 'Anonymous';
const msg = `${name}已退出`;
rd.messages.push({ type: 'system', text: msg });
if (activeRoomIndex === idx) addSystemMsg(msg, true);
}
}
}
// 更新在线用户列表和映射
// 记录旧用户ID集合
const oldUserIds = new Set((rd.userList || []).map(u => u.clientId));
// 新用户ID集合
const newUserIds = new Set(list.map(u => u.clientId));
// 检查退出用户(旧有,新没有),并调用 handleClientLeft
for (const oldId of oldUserIds) {
if (!newUserIds.has(oldId)) {
handleClientLeft(idx, oldId);
}
}
// 更新在线用户列表和映射
rd.userList = list;
rd.userMap = {};
list.forEach(u => { rd.userMap[u.clientId] = u; });
@@ -117,7 +111,6 @@ function handleClientList(idx, list, selfId) {
// 基准用户集合
rd.knownUserIds = new Set(list.map(u => u.clientId));
}
}
// 调整 handleClientSecured: 立即更新UI,仅在初始化完成后才处理加入提示
@@ -161,10 +154,13 @@ function handleClientLeft(idx, clientId) {
const rd = roomsData[idx];
if (!rd) return;
const user = rd.userMap[clientId];
// 退出提示逻辑:不再依赖 isInitialized,任何时候都提示
const name = user ? (user.userName || user.username || user.name || 'Anonymous') : 'Anonymous';
const msg = `${name}已退出`;
rd.messages.push({ type: 'system', text: msg });
if (activeRoomIndex === idx) addSystemMsg(msg, true);
rd.userList = rd.userList.filter(u => u.clientId !== clientId);
delete rd.userMap[clientId];
rd.messages.push({ type: 'system', text: exitMsg });
if (activeRoomIndex === idx) {
renderUserList();
}