fix
This commit is contained in:
parent
b1ba4edffc
commit
3469cfb332
@ -100,11 +100,22 @@ export default function AskScreen() {
|
|||||||
|
|
||||||
useFocusEffect(
|
useFocusEffect(
|
||||||
useCallback(() => {
|
useCallback(() => {
|
||||||
|
// 添加一个标志来检查组件是否已卸载
|
||||||
|
let isMounted = true;
|
||||||
|
|
||||||
|
// 确保在组件挂载时才连接
|
||||||
|
if (isMounted) {
|
||||||
webSocketManager.connect();
|
webSocketManager.connect();
|
||||||
|
}
|
||||||
|
|
||||||
const handleChatStream = (message: WsMessage) => {
|
const handleChatStream = (message: WsMessage) => {
|
||||||
|
// 确保组件仍然挂载
|
||||||
|
if (!isMounted) return;
|
||||||
|
|
||||||
if (message.type === 'ChatStream') {
|
if (message.type === 'ChatStream') {
|
||||||
setUserMessages(prevMessages => {
|
setUserMessages(prevMessages => {
|
||||||
|
// 使用 try-catch 包装以防止错误导致应用崩溃
|
||||||
|
try {
|
||||||
const lastMessage = prevMessages[prevMessages.length - 1];
|
const lastMessage = prevMessages[prevMessages.length - 1];
|
||||||
|
|
||||||
// 检查是否是有效的助手消息
|
// 检查是否是有效的助手消息
|
||||||
@ -129,7 +140,7 @@ export default function AskScreen() {
|
|||||||
content: lastMessage.content + message.chunk
|
content: lastMessage.content + message.chunk
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} else {
|
} else if (Array.isArray(lastMessage.content)) {
|
||||||
// 如果 content 是数组,则更新第一个 text 部分
|
// 如果 content 是数组,则更新第一个 text 部分
|
||||||
const textPartIndex = lastMessage.content.findIndex(p => p.type === 'text');
|
const textPartIndex = lastMessage.content.findIndex(p => p.type === 'text');
|
||||||
if (textPartIndex !== -1) {
|
if (textPartIndex !== -1) {
|
||||||
@ -147,13 +158,23 @@ export default function AskScreen() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return newMessages;
|
return newMessages;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('处理 ChatStream 消息时出错:', error);
|
||||||
|
// 发生错误时返回原始消息数组
|
||||||
|
return prevMessages;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleChatStreamEnd = (message: WsMessage) => {
|
const handleChatStreamEnd = (message: WsMessage) => {
|
||||||
|
// 确保组件仍然挂载
|
||||||
|
if (!isMounted) return;
|
||||||
|
|
||||||
if (message.type === 'ChatStreamEnd') {
|
if (message.type === 'ChatStreamEnd') {
|
||||||
setUserMessages(prevMessages => {
|
setUserMessages(prevMessages => {
|
||||||
|
// 使用 try-catch 包装以防止错误导致应用崩溃
|
||||||
|
try {
|
||||||
const lastMessage = prevMessages[prevMessages.length - 1];
|
const lastMessage = prevMessages[prevMessages.length - 1];
|
||||||
|
|
||||||
// 检查是否是有效的助手消息
|
// 检查是否是有效的助手消息
|
||||||
@ -172,15 +193,25 @@ export default function AskScreen() {
|
|||||||
!(typeof m.content === 'string' && m.content === 'keepSearchIng')
|
!(typeof m.content === 'string' && m.content === 'keepSearchIng')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('处理 ChatStreamEnd 消息时出错:', error);
|
||||||
|
// 发生错误时返回原始消息数组
|
||||||
|
return prevMessages;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleError = (message: WsMessage) => {
|
const handleError = (message: WsMessage) => {
|
||||||
|
// 确保组件仍然挂载
|
||||||
|
if (!isMounted) return;
|
||||||
|
|
||||||
if (message.type === 'Error') {
|
if (message.type === 'Error') {
|
||||||
console.log(`WebSocket Error: ${message.code} - ${message.message}`);
|
console.log(`WebSocket Error: ${message.code} - ${message.message}`);
|
||||||
|
|
||||||
setUserMessages(prevMessages => {
|
setUserMessages(prevMessages => {
|
||||||
|
// 使用 try-catch 包装以防止错误导致应用崩溃
|
||||||
|
try {
|
||||||
const lastMessage = prevMessages[prevMessages.length - 1];
|
const lastMessage = prevMessages[prevMessages.length - 1];
|
||||||
|
|
||||||
// 检查是否是有效的助手消息且包含占位符
|
// 检查是否是有效的助手消息且包含占位符
|
||||||
@ -199,6 +230,11 @@ export default function AskScreen() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return newMessages;
|
return newMessages;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('处理 Error 消息时出错:', error);
|
||||||
|
// 发生错误时返回原始消息数组
|
||||||
|
return prevMessages;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -208,11 +244,16 @@ export default function AskScreen() {
|
|||||||
webSocketManager.subscribe('Error', handleError);
|
webSocketManager.subscribe('Error', handleError);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
// 设置组件卸载标志
|
||||||
|
isMounted = false;
|
||||||
|
|
||||||
|
// 清理订阅
|
||||||
webSocketManager.unsubscribe('ChatStream', handleChatStream);
|
webSocketManager.unsubscribe('ChatStream', handleChatStream);
|
||||||
webSocketManager.unsubscribe('ChatStreamEnd', handleChatStreamEnd);
|
webSocketManager.unsubscribe('ChatStreamEnd', handleChatStreamEnd);
|
||||||
webSocketManager.unsubscribe('Error', handleError);
|
webSocketManager.unsubscribe('Error', handleError);
|
||||||
|
|
||||||
// 可以在这里选择断开连接,或者保持连接以加快下次进入页面的速度
|
// 可以在这里选择断开连接,或者保持连接以加快下次进入页面的速度
|
||||||
// webSocketManager.disconnect();
|
webSocketManager.disconnect();
|
||||||
};
|
};
|
||||||
}, [t])
|
}, [t])
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user