feat: 修复了连接断开之后重连的逻辑
This commit is contained in:
@@ -407,7 +407,7 @@ const getMainPageData = async () => {
|
|||||||
|
|
||||||
/// =============对话↓================
|
/// =============对话↓================
|
||||||
// 初始化WebSocket
|
// 初始化WebSocket
|
||||||
const initWebSocket = () => {
|
const initWebSocket = async () => {
|
||||||
// 清理旧实例
|
// 清理旧实例
|
||||||
if (webSocketManager) {
|
if (webSocketManager) {
|
||||||
webSocketManager.destroy();
|
webSocketManager.destroy();
|
||||||
@@ -426,9 +426,10 @@ const initWebSocket = () => {
|
|||||||
|
|
||||||
// 连接成功回调
|
// 连接成功回调
|
||||||
onOpen: (event) => {
|
onOpen: (event) => {
|
||||||
|
console.log("WebSocket连接成功");
|
||||||
// 重置会话状态
|
// 重置会话状态
|
||||||
webSocketConnectStatus = true;
|
webSocketConnectStatus = true;
|
||||||
isSessionActive.value = true;
|
isSessionActive.value = false; // 连接成功时重置会话状态,避免影响新消息发送
|
||||||
},
|
},
|
||||||
|
|
||||||
// 连接断开回调
|
// 连接断开回调
|
||||||
@@ -441,9 +442,9 @@ const initWebSocket = () => {
|
|||||||
|
|
||||||
// 错误回调
|
// 错误回调
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
|
console.error("WebSocket错误:", error);
|
||||||
webSocketConnectStatus = false;
|
webSocketConnectStatus = false;
|
||||||
isSessionActive.value = false;
|
isSessionActive.value = false;
|
||||||
console.error("WebSocket错误:", error);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// 消息回调
|
// 消息回调
|
||||||
@@ -458,21 +459,24 @@ const initWebSocket = () => {
|
|||||||
getAgentId: () => agentId.value,
|
getAgentId: () => agentId.value,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
// 初始化连接
|
// 初始化连接
|
||||||
webSocketManager
|
await webSocketManager.connect();
|
||||||
.connect()
|
console.log("WebSocket连接初始化成功");
|
||||||
.then(() => {
|
|
||||||
webSocketConnectStatus = true;
|
webSocketConnectStatus = true;
|
||||||
})
|
return true;
|
||||||
.catch((error) => {
|
} catch (error) {
|
||||||
console.error("WebSocket连接失败:", error);
|
console.error("WebSocket连接失败:", error);
|
||||||
});
|
webSocketConnectStatus = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 处理WebSocket消息
|
// 处理WebSocket消息
|
||||||
const handleWebSocketMessage = (data) => {
|
const handleWebSocketMessage = (data) => {
|
||||||
const aiMsgIndex = chatMsgList.value.length - 1;
|
const aiMsgIndex = chatMsgList.value.length - 1;
|
||||||
if (!chatMsgList.value[aiMsgIndex] || aiMsgIndex < 0) {
|
if (!chatMsgList.value[aiMsgIndex] || aiMsgIndex < 0) {
|
||||||
|
console.error("处理WebSocket消息时找不到对应的AI消息项");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -515,10 +519,13 @@ const handleWebSocketMessage = (data) => {
|
|||||||
// 重置会话状态
|
// 重置会话状态
|
||||||
isSessionActive.value = false;
|
isSessionActive.value = false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 重置消息状态
|
// 重置消息状态
|
||||||
const resetMessageState = () => {};
|
const resetMessageState = () => {
|
||||||
|
// 重置当前会话消息ID
|
||||||
|
currentSessionMessageId = null;
|
||||||
|
};
|
||||||
|
|
||||||
// 初始化数据 首次数据加载的时候
|
// 初始化数据 首次数据加载的时候
|
||||||
const initData = () => {
|
const initData = () => {
|
||||||
@@ -536,13 +543,40 @@ const sendMessage = async (message, isInstruct = false) => {
|
|||||||
|
|
||||||
await checkToken();
|
await checkToken();
|
||||||
|
|
||||||
|
// 检查WebSocket连接状态,如果未连接,尝试重新连接
|
||||||
if (!webSocketConnectStatus) {
|
if (!webSocketConnectStatus) {
|
||||||
|
console.log("WebSocket未连接,尝试重新连接...");
|
||||||
|
// 显示加载提示
|
||||||
|
uni.showLoading({
|
||||||
|
title: "正在连接服务器...",
|
||||||
|
});
|
||||||
|
|
||||||
|
// 尝试重新初始化WebSocket连接
|
||||||
|
try {
|
||||||
|
await initWebSocket();
|
||||||
|
// 等待短暂时间确保连接建立
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
|
|
||||||
|
// 检查连接是否成功建立
|
||||||
|
if (!webSocketConnectStatus) {
|
||||||
|
uni.hideLoading();
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: "当前网络异常,请稍后重试",
|
title: "连接服务器失败,请稍后重试",
|
||||||
icon: "none",
|
icon: "none",
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
uni.hideLoading();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("重新连接WebSocket失败:", error);
|
||||||
|
uni.hideLoading();
|
||||||
|
uni.showToast({
|
||||||
|
title: "连接服务器失败,请稍后重试",
|
||||||
|
icon: "none",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isSessionActive.value) {
|
if (isSessionActive.value) {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
@@ -563,6 +597,8 @@ const sendMessage = async (message, isInstruct = false) => {
|
|||||||
};
|
};
|
||||||
chatMsgList.value.push(newMsg);
|
chatMsgList.value.push(newMsg);
|
||||||
inputMessage.value = "";
|
inputMessage.value = "";
|
||||||
|
// 发送消息后滚动到底部
|
||||||
|
setTimeoutScrollToBottom();
|
||||||
sendChat(message, isInstruct);
|
sendChat(message, isInstruct);
|
||||||
console.log("发送的新消息:", JSON.stringify(newMsg));
|
console.log("发送的新消息:", JSON.stringify(newMsg));
|
||||||
};
|
};
|
||||||
@@ -578,9 +614,11 @@ const sendWebSocketMessage = (messageType, messageContent, options = {}) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
webSocketManager.sendMessage(args);
|
// 直接调用webSocketManager的sendMessage方法,利用其内部的消息队列机制
|
||||||
|
// 即使当前连接断开,消息也会被加入队列,等待连接恢复后发送
|
||||||
|
const result = webSocketManager.sendMessage(args);
|
||||||
console.log(`WebSocket消息已发送 [类型:${messageType}]:`, args);
|
console.log(`WebSocket消息已发送 [类型:${messageType}]:`, args);
|
||||||
return true;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("发送WebSocket消息失败:", error);
|
console.error("发送WebSocket消息失败:", error);
|
||||||
isSessionActive.value = false;
|
isSessionActive.value = false;
|
||||||
@@ -590,9 +628,26 @@ const sendWebSocketMessage = (messageType, messageContent, options = {}) => {
|
|||||||
|
|
||||||
// 发送获取AI聊天消息
|
// 发送获取AI聊天消息
|
||||||
const sendChat = (message, isInstruct = false) => {
|
const sendChat = (message, isInstruct = false) => {
|
||||||
if (!webSocketManager || !webSocketManager.isConnected()) {
|
// 检查WebSocket管理器是否存在,如果不存在,尝试重新初始化
|
||||||
console.error("WebSocket未连接");
|
if (!webSocketManager) {
|
||||||
|
console.error("WebSocket管理器不存在,尝试重新初始化...");
|
||||||
|
initWebSocket();
|
||||||
|
// 短暂延迟后再次检查连接状态
|
||||||
|
setTimeout(() => {
|
||||||
|
if (webSocketManager && webSocketManager.isConnected()) {
|
||||||
|
// 连接成功后重新发送消息
|
||||||
|
sendChat(message, isInstruct);
|
||||||
|
} else {
|
||||||
|
console.error("WebSocket重新初始化失败");
|
||||||
isSessionActive.value = false;
|
isSessionActive.value = false;
|
||||||
|
// 更新AI消息状态为失败
|
||||||
|
const aiMsgIndex = chatMsgList.value.length - 1;
|
||||||
|
if (aiMsgIndex >= 0 && chatMsgList.value[aiMsgIndex].msgType === MessageRole.AI) {
|
||||||
|
chatMsgList.value[aiMsgIndex].msg = "发送消息失败,请重试";
|
||||||
|
chatMsgList.value[aiMsgIndex].isLoading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -615,6 +670,8 @@ const sendChat = (message, isInstruct = false) => {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
chatMsgList.value.push(aiMsg);
|
chatMsgList.value.push(aiMsg);
|
||||||
|
// 添加AI消息后滚动到底部
|
||||||
|
setTimeoutScrollToBottom();
|
||||||
const aiMsgIndex = chatMsgList.value.length - 1;
|
const aiMsgIndex = chatMsgList.value.length - 1;
|
||||||
|
|
||||||
// 发送消息
|
// 发送消息
|
||||||
@@ -678,6 +735,7 @@ const resetConfig = () => {
|
|||||||
|
|
||||||
// 重置消息状态
|
// 重置消息状态
|
||||||
resetMessageState();
|
resetMessageState();
|
||||||
|
isSessionActive.value = false;
|
||||||
|
|
||||||
// 清理定时器
|
// 清理定时器
|
||||||
if (holdKeyboardTimer.value) {
|
if (holdKeyboardTimer.value) {
|
||||||
|
|||||||
Reference in New Issue
Block a user