feat: 修复了连接断开之后重连的逻辑

This commit is contained in:
2025-12-01 16:39:55 +08:00
parent 172bae663b
commit c20563ecf5

View File

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