From 0faed19bd39d3850185638f3a93bc472b60c3151 Mon Sep 17 00:00:00 2001 From: shuaiplus <2327005759@qq.com> Date: Thu, 8 May 2025 10:34:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=B3=BB=E7=BB=9F=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E4=BB=A5=E9=80=9A=E7=9F=A5=E7=94=A8=E6=88=B7=E5=B7=B2?= =?UTF-8?q?=E9=80=80=E5=87=BA=EF=BC=8C=E5=AE=8C=E7=BE=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/main.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/web/main.js b/web/main.js index b86857e..254b688 100644 --- a/web/main.js +++ b/web/main.js @@ -81,7 +81,21 @@ function createUserItem(user, isMe) { function handleClientList(idx, list, selfId) { const rd = roomsData[idx]; if (!rd) return; - + // 检测退出用户:对比旧列表和新列表 + const oldList = rd.userList || []; + const oldIds = oldList.map(u => u.clientId); + const newIds = list.map(u => u.clientId); + const exitedIds = oldIds.filter(id => !newIds.includes(id)); + exitedIds.forEach(clientId => { + const user = rd.userMap[clientId] || oldList.find(u=>u.clientId===clientId); + const userName = user ? (user.userName||user.username||user.name) : 'Anonymous'; + const exitMsg = `${userName}已退出`; + // 推送系统消息到历史记录 + rd.messages.push({ type: 'system', text: exitMsg }); + // 仅在当前房间下渲染,不重复添加到历史记录 + if (activeRoomIndex === idx) addSystemMsg(exitMsg, true); + }); + // 更新列表 rd.userList = list; rd.userMap = {}; list.forEach(u => { @@ -89,7 +103,6 @@ function handleClientList(idx, list, selfId) { }); rd.myId = selfId; if (activeRoomIndex === idx) renderUserList(); - } // 新用户上线或资料变更 function handleClientSecured(idx, user) { @@ -108,9 +121,17 @@ function handleClientSecured(idx, user) { function handleClientLeft(idx, clientId) { const rd = roomsData[idx]; if (!rd) return; + const user = rd.userMap[clientId]; + const userName = user ? (user.userName || user.username || user.name || 'Anonymous') : 'Anonymous'; rd.userList = rd.userList.filter(u => u.clientId !== clientId); delete rd.userMap[clientId]; - if (activeRoomIndex === idx) renderUserList(); + const exitMsg = `${userName}已退出`; + // 保存系统消息到对应房间历史 + rd.messages.push({ type: 'system', text: exitMsg }); + if (activeRoomIndex === idx) { + addSystemMsg(exitMsg, true); + renderUserList(); + } } function setStatus(text) {