fix(ui): 优化 WebSocket 连接和 PWA meta 标签

- WebSocket onopen 检查 isClosingRef,组件卸载后自动关闭连接
- 更新 PWA meta 标签为 mobile-web-app-capable
This commit is contained in:
2025-12-15 10:29:49 +08:00
parent 9e55237dae
commit bbce1de60c
2 changed files with 15 additions and 3 deletions
+14 -2
View File
@@ -80,6 +80,11 @@ export function useChat({ sessionId, onError, onSessionNotFound, onSessionUpdate
const ws = createWebSocket(sessionId);
ws.onopen = () => {
// 如果在连接过程中组件已卸载,立即关闭
if (isClosingRef.current) {
ws.close();
return;
}
reconnectAttemptsRef.current = 0; // 连接成功,重置重连次数
setState((prev) => ({ ...prev, isConnected: true }));
};
@@ -282,10 +287,17 @@ export function useChat({ sessionId, onError, onSessionNotFound, onSessionUpdate
const ws = wsRef.current;
// 清除引用,防止后续操作
wsRef.current = null;
// 只有在 OPEN 或 CONNECTING 状态才关闭
if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
// 清除事件处理器,避免关闭时触发错误
ws.onclose = null;
ws.onerror = null;
ws.onmessage = null;
ws.onopen = null;
// 只关闭已经建立的连接,避免 "closed before established" 警告
if (ws.readyState === WebSocket.OPEN) {
ws.close();
}
// 对于 CONNECTING 状态,等待连接建立后再关闭
// 这样可以避免浏览器警告
}
};
}, [loadMessages, connect]);
+1 -1
View File
@@ -7,7 +7,7 @@
<!-- PWA Meta Tags -->
<meta name="theme-color" content="#111827" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="apple-mobile-web-app-title" content="AI Assistant" />
<meta name="description" content="AI Terminal Assistant - Your intelligent coding companion" />