fix(WebTerminal): work

This commit is contained in:
CyberL1 2025-01-26 19:00:05 +01:00
parent 3cd59266c2
commit 85d70aa393

View File

@ -16,10 +16,10 @@ export default function WebTerminal() {
document.title = `Terminal: ${container.name}`;
if (terminalRef.current) {
const terminal = new Terminal({ rows: 67 });
const terminal = new Terminal({ rows: 67, convertEol: true });
const socket = new WebSocket(
`ws://127.0.0.1:3000/containers/${container.id}/terminal`, // TODO: Replace this with dlinux ssh connector
`wss://session.ssh.surf?token=${localStorage.getItem("key")?.toString()}`,
);
const fitAddon = new FitAddon();
@ -31,20 +31,50 @@ export default function WebTerminal() {
terminal.write("Connecting to the container\r\n");
socket.onopen = () => {
const attachAddon = new AttachAddon(socket);
const attachAddon = new AttachAddon(socket, { bidirectional: false });
terminal.loadAddon(attachAddon);
terminal.clear();
terminal.focus();
socket.send(
JSON.stringify({
rows: terminal.rows,
cols: terminal.cols,
}),
);
};
let line = "";
let cursor = 0;
let busy = false;
terminal.onData((data) => {
if (socket.readyState === WebSocket.OPEN) {
if (busy) {
return;
}
switch (data) {
case "\r": // Enter
socket.send(line);
cursor = 0;
line = "";
break;
case "\x7f": // Backspace
if (cursor > 0) {
terminal.write("\b \b");
if (line.length > 0) {
line = line.substring(0, line.length - 1);
}
cursor--;
}
break;
default:
// Check if character is printable
if (data.charCodeAt(0) >= 32 && data.charCodeAt(0) <= 127) {
cursor++;
line += data;
terminal.write(data);
}
}
}
});
window.addEventListener("resize", () => {
fitAddon.fit();
});