From 5849411e89dd33105d8fdd4fbe4e239e31ba35b3 Mon Sep 17 00:00:00 2001 From: zoujing Date: Tue, 16 Sep 2025 15:45:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20WebSocket=E9=93=BE=E6=8E=A5=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/chat/ChatMainList.vue | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pages/chat/ChatMainList.vue b/pages/chat/ChatMainList.vue index b027cd4..334d918 100644 --- a/pages/chat/ChatMainList.vue +++ b/pages/chat/ChatMainList.vue @@ -212,6 +212,8 @@ let commonType = ""; // WebSocket 相关 let webSocketManager = null; +/// WebSocket 连接状态 +let webSocketConnectStatus = false; // 打字机管理器 let typewriterManager = null; // 当前会话的消息ID,用于保持发送和终止的messageId一致 @@ -442,12 +444,14 @@ const initWebSocket = () => { // 连接成功回调 onOpen: (event) => { // 重置会话状态 + webSocketConnectStatus = true; isSessionActive.value = true; }, // 连接断开回调 onClose: (event) => { console.error("WebSocket连接断开:", event); + webSocketConnectStatus = false; // 停止当前会话 isSessionActive.value = false; // 停止打字机效果 @@ -458,6 +462,7 @@ const initWebSocket = () => { // 错误回调 onError: (error) => { + webSocketConnectStatus = false; isSessionActive.value = false; console.error("WebSocket错误:", error); }, @@ -475,7 +480,9 @@ const initWebSocket = () => { }); // 初始化连接 - webSocketManager.connect().catch((error) => { + webSocketManager.connect().then(() => { + webSocketConnectStatus = true; + }).catch((error) => { console.error("WebSocket连接失败:", error); }); }; @@ -601,6 +608,15 @@ const initData = () => { // 发送消息的参数拼接 const sendMessage = (message, isInstruct = false) => { console.log("发送的消息:", message); + + if (!webSocketConnectStatus) { + uni.showToast({ + title: "当前网络异常,请稍后重试", + icon: "none", + }); + return; + } + if (!appStore.hasToken) { console.log("没有token,跳转到登录页"); goLogin();