diff --git a/web/main.js b/web/main.js index b89e274..8427759 100644 --- a/web/main.js +++ b/web/main.js @@ -81,7 +81,23 @@ 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); + } + } + } + // 更新在线用户列表和映射 rd.userList = list; rd.userMap = {}; list.forEach(u => { rd.userMap[u.clientId] = u; }); @@ -98,9 +114,7 @@ function handleClientList(idx, list, selfId) { // 基准用户集合 rd.knownUserIds = new Set(list.map(u => u.clientId)); } - } - // 调整 handleClientSecured: 立即更新UI,仅在初始化完成后才处理加入提示 function handleClientSecured(idx, user) { const rd = roomsData[idx];