fix: K8s终端bash不可用时抑制重复的'连接已断开'提示

This commit is contained in:
attacker committed 2026-06-23 16:46:33 +00:00
1 parent d1c557bb6c
commit e76061661b
1 file changed
+5 -4
@@ -55,6 +55,7 @@ export function PodExecTerminal({
const wsRef = useRef<WebSocket | null>(null);
const fitRef = useRef<FitAddon | null>(null);
const mountedRef = useRef(false);
const errorShownRef = useRef(false);
const [connState, setConnState] = useState<ConnState>('idle');
const [fullscreen, setFullscreen] = useState(false);
@@ -118,13 +119,12 @@ export function PodExecTerminal({
try {
const msg = JSON.parse(event.data);
if (msg.status === 0 && typeof msg.data === 'string') {
console.debug('[PodExec:ws] RX stdout, len=', msg.data.length);
term.write(msg.data);
} else if (msg.status === 2 || msg.error) {
const errMsg = msg.data || msg.message || msg.error || '执行错误';
console.warn('[PodExec] error:', errMsg.substring(0, 200));
if (errMsg.includes('not found') && shell === 'bash') {
term.writeln('\r\n\x1b[33m⚠ bash 不可用,请切换 /bin/sh 后重连。\x1b[0m');
errorShownRef.current = true;
return;
}
term.write(errMsg.includes('\x1b') ? errMsg : `\r\n\x1b[31m${errMsg}\x1b[0m\r\n`);
@@ -135,9 +135,10 @@ export function PodExecTerminal({
};
ws.onclose = (ev) => {
console.log('[PodExec:connect] WS closed: code=', ev.code, 'reason=', ev.reason, 'wasClean=', ev.wasClean);
setConnState((s) => (s === 'error' ? s : 'closed'));
term.write('\r\n\x1b[33m[连接已断开]\x1b[0m\r\n');
if (!errorShownRef.current) {
term.write('\r\n\x1b[33m[连接已关闭]\x1b[0m\r\n');
}
};
ws.onerror = (ev) => {