feat: 发送消息的优化

This commit is contained in:
2026-01-08 15:03:43 +08:00
parent 7239313f0f
commit fb15b8aec1

View File

@@ -148,8 +148,8 @@ let commonType = "";
// WebSocket 相关 // WebSocket 相关
let webSocketManager = null; let webSocketManager = null;
/// WebSocket 连接状态 /// 使用统一的连接状态判断函数,避免状态不同步
let webSocketConnectStatus = false; const isWsConnected = () => !!(webSocketManager && typeof webSocketManager.isConnected === "function" && webSocketManager.isConnected());
// 当前会话的消息ID用于保持发送和终止的messageId一致 // 当前会话的消息ID用于保持发送和终止的messageId一致
let currentSessionMessageId = null; let currentSessionMessageId = null;
@@ -251,7 +251,7 @@ const sendMessageAction = (inputText) => {
/// 添加通知 /// 添加通知
const addNoticeListener = () => { const addNoticeListener = () => {
uni.$on(NOTICE_EVENT_LOGIN_SUCCESS, () => { uni.$on(NOTICE_EVENT_LOGIN_SUCCESS, () => {
if (!webSocketConnectStatus) { if (!isWsConnected()) {
initHandler(); initHandler();
} }
}); });
@@ -375,14 +375,12 @@ const initWebSocket = async () => {
onOpen: (event) => { onOpen: (event) => {
console.log("WebSocket连接成功"); console.log("WebSocket连接成功");
// 重置会话状态 // 重置会话状态
webSocketConnectStatus = true;
isSessionActive.value = false; // 连接成功时重置会话状态,避免影响新消息发送 isSessionActive.value = false; // 连接成功时重置会话状态,避免影响新消息发送
}, },
// 连接断开回调 // 连接断开回调
onClose: (event) => { onClose: (event) => {
console.error("WebSocket连接断开:", event); console.error("WebSocket连接断开:", event);
webSocketConnectStatus = false;
// 停止当前会话 // 停止当前会话
isSessionActive.value = false; isSessionActive.value = false;
}, },
@@ -390,7 +388,6 @@ const initWebSocket = async () => {
// 错误回调 // 错误回调
onError: (error) => { onError: (error) => {
console.error("WebSocket错误:", error); console.error("WebSocket错误:", error);
webSocketConnectStatus = false;
isSessionActive.value = false; isSessionActive.value = false;
}, },
@@ -410,11 +407,9 @@ const initWebSocket = async () => {
// 初始化连接 // 初始化连接
await webSocketManager.connect(); await webSocketManager.connect();
console.log("WebSocket连接初始化成功"); console.log("WebSocket连接初始化成功");
webSocketConnectStatus = true;
return true; return true;
} catch (error) { } catch (error) {
console.error("WebSocket连接失败:", error); console.error("WebSocket连接失败:", error);
webSocketConnectStatus = false;
return false; return false;
} }
}; };
@@ -429,7 +424,7 @@ const handleWebSocketMessage = (data) => {
// 确保消息内容是字符串类型 // 确保消息内容是字符串类型
if (data.content && typeof data.content !== "string") { if (data.content && typeof data.content !== "string") {
data.content = String(data.content); data.content = JSON.stringify(data.content);
} }
// 直接拼接内容到AI消息 // 直接拼接内容到AI消息
@@ -491,7 +486,7 @@ const sendMessage = async (message, isInstruct = false) => {
await checkToken(); await checkToken();
// 检查WebSocket连接状态如果未连接尝试重新连接 // 检查WebSocket连接状态如果未连接尝试重新连接
if (!webSocketConnectStatus) { if (!isWsConnected()) {
console.log("WebSocket未连接尝试重新连接..."); console.log("WebSocket未连接尝试重新连接...");
// 显示加载提示 // 显示加载提示
uni.showLoading({ uni.showLoading({
@@ -505,7 +500,7 @@ const sendMessage = async (message, isInstruct = false) => {
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise(resolve => setTimeout(resolve, 1000));
// 检查连接是否成功建立 // 检查连接是否成功建立
if (!webSocketConnectStatus) { if (!isWsConnected()) {
uni.hideLoading(); uni.hideLoading();
uni.showToast({ uni.showToast({
title: "连接服务器失败,请稍后重试", title: "连接服务器失败,请稍后重试",
@@ -551,7 +546,7 @@ const sendMessage = async (message, isInstruct = false) => {
}; };
// 通用WebSocket消息发送函数 // 通用WebSocket消息发送函数
const sendWebSocketMessage = (messageType, messageContent, options = {}) => { const sendWebSocketMessage = (messageType, messageContent) => {
const args = { const args = {
conversationId: conversationId.value, conversationId: conversationId.value,
agentId: agentId.value, agentId: agentId.value,
@@ -636,7 +631,7 @@ const stopRequest = () => {
console.log("停止请求"); console.log("停止请求");
// 发送中断消息给服务器 (messageType=2) // 发送中断消息给服务器 (messageType=2)
sendWebSocketMessage(2, "stop_request", { silent: true }); sendWebSocketMessage(2, "stop_request");
// 直接将AI消息状态设为停止 // 直接将AI消息状态设为停止
const aiMsgIndex = chatMsgList.value.length - 1; const aiMsgIndex = chatMsgList.value.length - 1;
@@ -677,7 +672,6 @@ const resetConfig = () => {
if (webSocketManager) { if (webSocketManager) {
webSocketManager.destroy(); webSocketManager.destroy();
webSocketManager = null; webSocketManager = null;
webSocketConnectStatus = false;
} }
// 重置消息状态 // 重置消息状态