fix: K8s终端bash不可用时抑制重复的'连接已断开'提示
This commit is contained in:
1 parent
d1c557bb6c
commit
e76061661b
1 file changed
+5
-4
@@ -55,6 +55,7 @@ export function PodExecTerminal({
|
|||||||
const wsRef = useRef<WebSocket | null>(null);
|
const wsRef = useRef<WebSocket | null>(null);
|
||||||
const fitRef = useRef<FitAddon | null>(null);
|
const fitRef = useRef<FitAddon | null>(null);
|
||||||
const mountedRef = useRef(false);
|
const mountedRef = useRef(false);
|
||||||
|
const errorShownRef = useRef(false);
|
||||||
|
|
||||||
const [connState, setConnState] = useState<ConnState>('idle');
|
const [connState, setConnState] = useState<ConnState>('idle');
|
||||||
const [fullscreen, setFullscreen] = useState(false);
|
const [fullscreen, setFullscreen] = useState(false);
|
||||||
@@ -118,13 +119,12 @@ export function PodExecTerminal({
|
|||||||
try {
|
try {
|
||||||
const msg = JSON.parse(event.data);
|
const msg = JSON.parse(event.data);
|
||||||
if (msg.status === 0 && typeof msg.data === 'string') {
|
if (msg.status === 0 && typeof msg.data === 'string') {
|
||||||
console.debug('[PodExec:ws] RX stdout, len=', msg.data.length);
|
|
||||||
term.write(msg.data);
|
term.write(msg.data);
|
||||||
} else if (msg.status === 2 || msg.error) {
|
} else if (msg.status === 2 || msg.error) {
|
||||||
const errMsg = msg.data || msg.message || 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') {
|
if (errMsg.includes('not found') && shell === 'bash') {
|
||||||
term.writeln('\r\n\x1b[33m⚠ bash 不可用,请切换 /bin/sh 后重连。\x1b[0m');
|
term.writeln('\r\n\x1b[33m⚠ bash 不可用,请切换 /bin/sh 后重连。\x1b[0m');
|
||||||
|
errorShownRef.current = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
term.write(errMsg.includes('\x1b') ? errMsg : `\r\n\x1b[31m${errMsg}\x1b[0m\r\n`);
|
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) => {
|
ws.onclose = (ev) => {
|
||||||
console.log('[PodExec:connect] WS closed: code=', ev.code, 'reason=', ev.reason, 'wasClean=', ev.wasClean);
|
|
||||||
setConnState((s) => (s === 'error' ? s : 'closed'));
|
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) => {
|
ws.onerror = (ev) => {
|
||||||
|
|||||||
Reference in new issue
Block a user