fix: xterminal run script error

This commit is contained in:
engigu
2026-01-14 18:20:44 +08:00
parent f14102ef45
commit 57025e1eb4
2 changed files with 16 additions and 6 deletions
+15 -3
View File
@@ -39,10 +39,15 @@ function initTerminal(forceConnect = false) {
terminal.dispose() terminal.dispose()
terminal = null terminal = null
} }
// 确保旧的 WebSocket 完全关闭
if (ws) { if (ws) {
ws.close() if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
ws.close()
}
ws = null ws = null
} }
inputBuffer = '' inputBuffer = ''
isPtyMode = false isPtyMode = false
@@ -74,7 +79,10 @@ function initTerminal(forceConnect = false) {
// autoConnect 或者强制连接时才连接 // autoConnect 或者强制连接时才连接
if (props.autoConnect || forceConnect) { if (props.autoConnect || forceConnect) {
connectWebSocket() // 延迟连接,确保终端完全初始化
setTimeout(() => {
connectWebSocket()
}, 100)
} }
// 清除当前输入行(Windows 模式用) // 清除当前输入行(Windows 模式用)
@@ -194,13 +202,17 @@ function reconnect() {
function dispose() { function dispose() {
if (ws) { if (ws) {
ws.close() if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
ws.close()
}
ws = null ws = null
} }
if (terminal) { if (terminal) {
terminal.dispose() terminal.dispose()
terminal = null terminal = null
} }
inputBuffer = ''
isPtyMode = false
} }
function handleResize() { function handleResize() {
+1 -3
View File
@@ -197,9 +197,7 @@ async function runScript() {
showTerminalDialog.value = true showTerminalDialog.value = true
// 等待 DOM 更新后初始化终端 // 等待 DOM 更新后初始化终端
await nextTick() await nextTick()
setTimeout(() => { terminalRef.value?.initTerminal(true)
terminalRef.value?.initTerminal(true)
}, 100)
} }
function closeTerminal() { function closeTerminal() {