feat: WebSocket链接状态管理
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user