debug: add WebSocket stability delay and enhanced checks
- Added 100ms delay before sending command to ensure WebSocket stability - Added detailed WebSocket state logging (CONNECTING, OPEN, CLOSING, CLOSED) - Added bufferedAmount check to wait for pending sends - Wrapped ws.send() in try-catch for better error reporting - Split terminal and ws existence checks for clearer debugging
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -699,28 +699,55 @@ class TerminalManager {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.debugLog('CMD', `Terminal ${terminalId} is ready! Sending command.`);
|
this.debugLog('CMD', `Terminal ${terminalId} is ready!`);
|
||||||
|
|
||||||
|
// Small delay to ensure WebSocket is stable after switching terminals
|
||||||
|
this.debugLog('CMD', `Waiting 100ms for WebSocket to stabilize...`);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 100));
|
||||||
|
|
||||||
const terminal = this.terminals.get(terminalId);
|
const terminal = this.terminals.get(terminalId);
|
||||||
if (!terminal || !terminal.ws || terminal.ws.readyState !== WebSocket.OPEN) {
|
if (!terminal) {
|
||||||
this.debugLog('ERROR', `Cannot send - WebSocket not ready`, {
|
this.debugLog('ERROR', `Terminal ${terminalId} not found in map`);
|
||||||
hasTerminal: !!terminal,
|
return;
|
||||||
hasWs: !!terminal?.ws,
|
}
|
||||||
wsState: terminal?.ws?.readyState,
|
|
||||||
stateName: terminal?.ws?.readyState === WebSocket.OPEN ? 'OPEN' : 'NOT OPEN'
|
if (!terminal.ws) {
|
||||||
|
this.debugLog('ERROR', `WebSocket not set for terminal ${terminalId}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check WebSocket state
|
||||||
|
const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
|
||||||
|
this.debugLog('CMD', `WebSocket state: ${readyStates[terminal.ws.readyState]} (${terminal.ws.readyState})`);
|
||||||
|
|
||||||
|
if (terminal.ws.readyState !== WebSocket.OPEN) {
|
||||||
|
this.debugLog('ERROR', `Cannot send - WebSocket not open`, {
|
||||||
|
wsState: terminal.ws.readyState,
|
||||||
|
stateName: readyStates[terminal.ws.readyState]
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if WebSocket has any buffered amount (indicating pending sends)
|
||||||
|
if (terminal.ws.bufferedAmount > 0) {
|
||||||
|
this.debugLog('CMD', `WebSocket has ${terminal.ws.bufferedAmount} bytes buffered, waiting...`);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 200));
|
||||||
|
}
|
||||||
|
|
||||||
// Send command to terminal
|
// Send command to terminal
|
||||||
const message = JSON.stringify({
|
const message = JSON.stringify({
|
||||||
type: 'input',
|
type: 'input',
|
||||||
data: command
|
data: command
|
||||||
});
|
});
|
||||||
this.debugLog('CMD', `Sending to WebSocket: ${message}`);
|
this.debugLog('CMD', `Sending to WebSocket: ${message}`);
|
||||||
terminal.ws.send(message);
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
terminal.ws.send(message);
|
||||||
this.debugLog('CMD', `Command sent to terminal ${terminalId}: ${command.trim()}`);
|
this.debugLog('CMD', `Command sent to terminal ${terminalId}: ${command.trim()}`);
|
||||||
|
} catch (error) {
|
||||||
|
this.debugLog('ERROR', `Failed to send command`, { error: error.message, name: error.name });
|
||||||
|
showToast(`Failed to send command: ${error.message}`, 'error');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user