feat: 延迟初始化
This commit is contained in:
parent
ac80003c5d
commit
136346e189
@ -4,7 +4,7 @@ import AskHello from "@/components/ask/hello";
|
||||
import SendMessage from "@/components/ask/send";
|
||||
import { ThemedText } from "@/components/ThemedText";
|
||||
import { fetchApi } from "@/lib/server-api-util";
|
||||
import { getWebSocketErrorMessage, webSocketManager, WsMessage } from "@/lib/websocket-util";
|
||||
import { getWebSocketErrorMessage, getWebSocketManager, WsMessage } from "@/lib/websocket-util";
|
||||
import { Assistant, Message } from "@/types/ask";
|
||||
import { useFocusEffect, useLocalSearchParams, useRouter } from "expo-router";
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
@ -100,13 +100,11 @@ export default function AskScreen() {
|
||||
|
||||
useFocusEffect(
|
||||
useCallback(() => {
|
||||
// 添加一个标志来检查组件是否已卸载
|
||||
let isMounted = true;
|
||||
|
||||
// 确保在组件挂载时才连接
|
||||
if (isMounted) {
|
||||
webSocketManager.connect();
|
||||
}
|
||||
const webSocketManager = getWebSocketManager();
|
||||
webSocketManager.connect();
|
||||
|
||||
let isMounted = true;
|
||||
|
||||
const handleChatStream = (message: WsMessage) => {
|
||||
// 确保组件仍然挂载
|
||||
|
||||
@ -12,7 +12,7 @@ import {
|
||||
View
|
||||
} from 'react-native';
|
||||
|
||||
import { webSocketManager, WsMessage } from '@/lib/websocket-util';
|
||||
import { getWebSocketManager, WsMessage } from '@/lib/websocket-util';
|
||||
import { Message } from '@/types/ask';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { ThemedText } from '../ThemedText';
|
||||
@ -124,6 +124,7 @@ export default function SendMessage(props: Props) {
|
||||
const typedHandleChatStreamEnd = handleChatStreamEnd as (message: WsMessage) => void;
|
||||
const typedHandleChatResponse = handleChatResponse as (message: WsMessage) => void;
|
||||
|
||||
const webSocketManager = getWebSocketManager();
|
||||
webSocketManager.subscribe('ChatStream', typedHandleChatStream);
|
||||
webSocketManager.subscribe('ChatStreamEnd', typedHandleChatStreamEnd);
|
||||
webSocketManager.subscribe('ChatResponse', typedHandleChatResponse);
|
||||
@ -193,6 +194,7 @@ export default function SendMessage(props: Props) {
|
||||
if (!currentSessionId) {
|
||||
currentSessionId = await createNewConversation(text);
|
||||
setConversationId(currentSessionId);
|
||||
const webSocketManager = getWebSocketManager();
|
||||
webSocketManager.send({
|
||||
type: 'Chat',
|
||||
session_id: currentSessionId,
|
||||
@ -204,6 +206,7 @@ export default function SendMessage(props: Props) {
|
||||
|
||||
// 通过 WebSocket 发送消息
|
||||
if (currentSessionId) {
|
||||
const webSocketManager = getWebSocketManager();
|
||||
webSocketManager.send({
|
||||
type: 'Chat',
|
||||
session_id: currentSessionId,
|
||||
|
||||
@ -312,7 +312,17 @@ class WebSocketManager {
|
||||
}
|
||||
|
||||
// 导出一个单例,确保整个应用共享同一个 WebSocket 连接
|
||||
export const webSocketManager = new WebSocketManager();
|
||||
let webSocketManagerInstance: WebSocketManager | null = null;
|
||||
|
||||
export const getWebSocketManager = (): WebSocketManager => {
|
||||
if (!webSocketManagerInstance) {
|
||||
webSocketManagerInstance = new WebSocketManager();
|
||||
}
|
||||
return webSocketManagerInstance;
|
||||
};
|
||||
|
||||
// 为了向后兼容,仍然导出实例
|
||||
export const webSocketManager = getWebSocketManager();
|
||||
|
||||
|
||||
// webscoket 错误映射
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user