From 9a1795f23350cdba21c3023f13123dc1bad00512 Mon Sep 17 00:00:00 2001 From: xsx <825657193@qq.com> Date: Thu, 9 Oct 2025 17:40:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=96=AD=E7=BA=BF=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=E5=90=8E=EF=BC=8C=E6=9C=89=E6=A6=82=E7=8E=87=E5=90=9E=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- im-uniapp/common/wssocket.js | 23 ++++++++++------------- im-uniapp/store/chatStore.js | 7 ++++++- im-web/src/store/chatStore.js | 7 ++++++- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/im-uniapp/common/wssocket.js b/im-uniapp/common/wssocket.js index 4476b69..5747dc5 100644 --- a/im-uniapp/common/wssocket.js +++ b/im-uniapp/common/wssocket.js @@ -15,12 +15,10 @@ let connect = (wsurl, token) => { lastConnectTime = new Date(); socketTask = uni.connectSocket({ url: wsurl, - success: (res) => { - console.log("websocket连接成功"); - }, + success: (res) => {}, fail: (e) => { console.log(e); - console.log("websocket连接失败,10s后重连"); + console.log("WebSocket连接失败,10s后重连"); setTimeout(() => { connect(); }, 10000) @@ -28,7 +26,7 @@ let connect = (wsurl, token) => { }); socketTask.onOpen((res) => { - console.log("WebSocket连接已打开"); + console.log("WebSocket连接成功"); isConnect = true; // 发送登录命令 let loginInfo = { @@ -64,9 +62,11 @@ let connect = (wsurl, token) => { closeCallBack && closeCallBack(res); }) - socketTask.onError((e) => { - console.log("ws错误:",e) + socketTask.onError(e => { + console.log("ws错误:", e) close(1006); + isConnect = false; + closeCallBack && closeCallBack(e); }) } @@ -93,11 +93,8 @@ let close = (code) => { socketTask.close({ code: code, complete: (res) => { - console.log("关闭websocket连接"); isConnect = false; - if (code != 3099) { - closeCallBack && closeCallBack(res);s - } + console.log("关闭websocket连接"); }, fail: (e) => { console.log("关闭websocket连接失败", e); @@ -108,8 +105,8 @@ let close = (code) => { // 心跳设置 let heartCheck = { - timeout: 20000, // 每段时间发送一次心跳包 这里设置为20s - timeoutObj: null, // 延时发送消息对象(启动心跳新建这个对象,收到消息后重置对象) + timeout: 20000, //每段时间发送一次心跳包 这里设置为20s + timeoutObj: null, //延时发送消息对象(启动心跳新建这个对象,收到消息后重置对象) start: function() { if (isConnect) { console.log('发送WebSocket心跳') diff --git a/im-uniapp/store/chatStore.js b/im-uniapp/store/chatStore.js index dd2e46d..56d8adc 100644 --- a/im-uniapp/store/chatStore.js +++ b/im-uniapp/store/chatStore.js @@ -361,7 +361,12 @@ export default defineStore('chatStore', { this.fliterMessage(chats, 5000, 1000); // #endif // 记录热数据索引位置 - chats.forEach(chat => chat.hotMinIdx = chat.messages.length); + chats.forEach(chat => { + if(!chat.hotMinIdx || chat.hotMinIdx != chat.messages.length){ + chat.hotMinIdx = chat.messages.length; + chat.stored = false; + } + }); // 将消息一次性装载回来 this.chats = chats; // 清空缓存,不再使用 diff --git a/im-web/src/store/chatStore.js b/im-web/src/store/chatStore.js index 5a5d691..f3658b1 100644 --- a/im-web/src/store/chatStore.js +++ b/im-web/src/store/chatStore.js @@ -360,7 +360,12 @@ export default defineStore('chatStore', { this.fliterMessage(chats, 10000, 1000) } // 记录热数据索引位置 - chats.forEach(chat => chat.hotMinIdx = chat.messages.length); + chats.forEach(chat => { + if(!chat.hotMinIdx || chat.hotMinIdx != chat.messages.length){ + chat.hotMinIdx = chat.messages.length; + chat.stored = false; + } + }); // 将消息一次性装载回来 this.chats = chats; // 清空缓存