Nano and timeout error fix #2

This commit is contained in:
LukeGus
2024-12-04 21:30:37 -06:00
parent 1139160e48
commit 246bb8fb26
2 changed files with 56 additions and 6 deletions

View File

@@ -17,6 +17,19 @@ wss.on('connection', (ws) => {
let conn = null; // Declare SSH client outside to manage lifecycle let conn = null; // Declare SSH client outside to manage lifecycle
// Ping-Pong for WebSocket Keep-Alives
const interval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
ws.ping();
} else {
clearInterval(interval);
}
}, 15000); // Send a ping every 15 seconds
ws.on('pong', () => {
console.log('Received pong from client');
});
ws.on('message', (message) => { ws.on('message', (message) => {
try { try {
const data = JSON.parse(message); // Try parsing the incoming message as JSON const data = JSON.parse(message); // Try parsing the incoming message as JSON
@@ -55,8 +68,21 @@ wss.on('connection', (ws) => {
// When the WebSocket client sends a message (from terminal input), forward it to the SSH stream // When the WebSocket client sends a message (from terminal input), forward it to the SSH stream
ws.on('message', (message) => { ws.on('message', (message) => {
console.log(`Received message from WebSocket: ${message}`); if (typeof message === 'string') {
stream.write(message); // Write the message (input) to the SSH shell try {
const resizeEvent = JSON.parse(message);
if (resizeEvent.type === 'resize') {
stream.setWindow(
resizeEvent.rows,
resizeEvent.cols,
resizeEvent.height,
resizeEvent.width
);
}
} catch {
stream.write(message);
}
}
}); });
}); });
}).on('error', (err) => { }).on('error', (err) => {
@@ -88,6 +114,7 @@ wss.on('connection', (ws) => {
// Handle WebSocket close event // Handle WebSocket close event
ws.on('close', () => { ws.on('close', () => {
console.log('WebSocket closed'); console.log('WebSocket closed');
clearInterval(interval);
if (conn) { if (conn) {
conn.end(); // Close SSH connection when WebSocket client disconnects conn.end(); // Close SSH connection when WebSocket client disconnects
} }

View File

@@ -29,7 +29,6 @@ const App = () => {
fitAddon.current = new FitAddon(); fitAddon.current = new FitAddon();
terminal.current.loadAddon(fitAddon.current); terminal.current.loadAddon(fitAddon.current);
terminal.current.open(terminalRef.current); terminal.current.open(terminalRef.current);
terminal.current.onData((data) => { terminal.current.onData((data) => {
@@ -40,11 +39,29 @@ const App = () => {
// Resize terminal to fit the container initially // Resize terminal to fit the container initially
const resizeTerminal = () => { const resizeTerminal = () => {
fitAddon.current.fit(); if (terminalRef.current) {
fitAddon.current.fit();
notifyServerOfResize();
}
}; };
resizeTerminal();
// Adjust terminal size on window resize // Notify the server of terminal resize
const notifyServerOfResize = () => {
if (socket.current && socket.current.readyState === WebSocket.OPEN) {
const { rows, cols } = terminal.current;
socket.current.send(
JSON.stringify({
type: 'resize',
rows,
cols,
height: terminalRef.current.offsetHeight,
width: terminalRef.current.offsetWidth,
})
);
}
};
resizeTerminal();
window.addEventListener('resize', resizeTerminal); window.addEventListener('resize', resizeTerminal);
return () => { return () => {
@@ -88,6 +105,12 @@ const App = () => {
const handleSideBarHiding = () => { const handleSideBarHiding = () => {
setIsSideBarHidden((prevState) => !prevState); setIsSideBarHidden((prevState) => !prevState);
if (!isSideBarHidden) {
setTimeout(() => {
fitAddon.current.fit();
notifyServerOfResize();
}, 100); // Delay to ensure layout updates before resize
}
}; };
return ( return (