From 5141f220bdb5bc7cdd233d7290af5066ea97b211 Mon Sep 17 00:00:00 2001 From: shuaiplus <2327005759@qq.com> Date: Tue, 6 May 2025 18:06:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=88=BF=E9=97=B4=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=93=E6=9E=84=EF=BC=8C=E5=A2=9E=E5=8A=A0prevUserL?= =?UTF-8?q?ist=E5=AD=97=E6=AE=B5=EF=BC=9B=E4=BF=AE=E6=AD=A3=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=90=8D=E7=A7=B0=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=85=88=E4=BB=8EuserMap=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=EF=BC=9B=E6=94=B9=E8=BF=9B=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E7=A1=AE=E4=BF=9D=E7=94=A8=E6=88=B7=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E4=B8=8D=E4=B8=BA=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/main.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/web/main.js b/web/main.js index 399ee15..41b6924 100644 --- a/web/main.js +++ b/web/main.js @@ -6,7 +6,7 @@ import { createAvatarSVG } from './util.avatar.js'; let roomsData = []; let activeRoomIndex = -1; function getNewRoomData() { - return { roomName: '', userList: [], userMap: {}, myId: null, myUserName: '', chat: null, messages: [] }; + return { roomName: '', userList: [], userMap: {}, myId: null, myUserName: '', chat: null, messages: [], prevUserList: [] }; } // 切换房间并恢复上下文,更新 UI function switchRoom(index) { @@ -80,6 +80,7 @@ function createUserItem(user, isMe) { function handleClientList(idx, list, selfId) { const rd = roomsData[idx]; if (!rd) return; + rd.userList = list; rd.userMap = {}; list.forEach(u => { @@ -87,6 +88,7 @@ function handleClientList(idx, list, selfId) { }); rd.myId = selfId; if (activeRoomIndex === idx) renderUserList(); + } // 新用户上线或资料变更 function handleClientSecured(idx, user) { @@ -141,8 +143,16 @@ contentHtml = safeText; chatArea.scrollTop = chatArea.scrollHeight; } -function addOtherMsg(msg, userName = 'Anonymous', avatar = '', isHistory = false, msgType = 'text') { - if (!isHistory && activeRoomIndex >= 0) roomsData[activeRoomIndex].messages.push({ type: 'other', text: msg, userName, avatar, msgType }); +function addOtherMsg(msg, userName = '', avatar = '', isHistory = false, msgType = 'text') { + // 修正:优先从userMap查找用户名,避免Anonymous + if (!userName && activeRoomIndex >= 0) { + const rd = roomsData[activeRoomIndex]; + if (rd && msg && msg.clientId && rd.userMap[msg.clientId]) { + userName = rd.userMap[msg.clientId].userName || rd.userMap[msg.clientId].username || rd.userMap[msg.clientId].name || 'Anonymous'; + } + } + if (!userName) userName = 'Anonymous'; + if (!isHistory && activeRoomIndex >= 0) roomsData[activeRoomIndex].messages.push({ type: 'other', text: msg, userName, avatar, msgType }); const chatArea = document.getElementById('chat-area'); if (!chatArea) return; const bubbleWrap = document.createElement('div'); @@ -262,7 +272,12 @@ function joinRoom(userName, roomName, password, modal = null, onResult) { if (msg.userName === newRd.myUserName) return; // 判断消息类型 let msgType = msg.type || (msg.data && msg.data.startsWith('data:image/') ? 'image' : 'text'); - roomsData[idx].messages.push({ type: 'other', text: msg.data, userName: msg.userName, avatar: msg.userName, msgType }); + // 修正:如果msg.userName缺失,尝试从userMap查找 + let realUserName = msg.userName; + if (!realUserName && msg.clientId && newRd.userMap[msg.clientId]) { + realUserName = newRd.userMap[msg.clientId].userName || newRd.userMap[msg.clientId].username || newRd.userMap[msg.clientId].name; + } + roomsData[idx].messages.push({ type: 'other', text: msg.data, userName: realUserName, avatar: realUserName, msgType }); if (activeRoomIndex === idx) renderChatArea(); } };