diff --git a/web/src/components/XTerminal.vue b/web/src/components/XTerminal.vue index f9f8148..a3c8374 100644 --- a/web/src/components/XTerminal.vue +++ b/web/src/components/XTerminal.vue @@ -39,10 +39,15 @@ function initTerminal(forceConnect = false) { terminal.dispose() terminal = null } + + // 确保旧的 WebSocket 完全关闭 if (ws) { - ws.close() + if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) { + ws.close() + } ws = null } + inputBuffer = '' isPtyMode = false @@ -74,7 +79,10 @@ function initTerminal(forceConnect = false) { // autoConnect 或者强制连接时才连接 if (props.autoConnect || forceConnect) { - connectWebSocket() + // 延迟连接,确保终端完全初始化 + setTimeout(() => { + connectWebSocket() + }, 100) } // 清除当前输入行(Windows 模式用) @@ -194,13 +202,17 @@ function reconnect() { function dispose() { if (ws) { - ws.close() + if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) { + ws.close() + } ws = null } if (terminal) { terminal.dispose() terminal = null } + inputBuffer = '' + isPtyMode = false } function handleResize() { diff --git a/web/src/views/editor/Editor.vue b/web/src/views/editor/Editor.vue index 704bc2a..75a02c7 100644 --- a/web/src/views/editor/Editor.vue +++ b/web/src/views/editor/Editor.vue @@ -197,9 +197,7 @@ async function runScript() { showTerminalDialog.value = true // 等待 DOM 更新后初始化终端 await nextTick() - setTimeout(() => { - terminalRef.value?.initTerminal(true) - }, 100) + terminalRef.value?.initTerminal(true) } function closeTerminal() {