<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<link
rel="stylesheet"
href="http://localhost:8080/static/sandbox/xterm@5.3.0/css/xterm.css"
/>
<style>
html,
body,
#terminal {
width: 100%;
height: 100%;
margin: 0;
}
</style>
</head>
<body>
<div id="terminal"></div>
<script src="http://localhost:8080/static/sandbox/xterm@5.3.0/lib/xterm.js"></script>
<script src="http://localhost:8080/static/sandbox/xterm-addon-fit@0.8.0/lib/xterm-addon-fit.js"></script>
<script>
const term = new Terminal({ cursorBlink: true, convertEol: true });
const fitAddon = new FitAddon.FitAddon();
term.loadAddon(fitAddon);
term.open(document.getElementById("terminal"));
fitAddon.fit();
const ws = new WebSocket("ws://localhost:8080/v1/shell/ws");
ws.addEventListener("open", () => {
ws.send(JSON.stringify({
type: "resize",
data: { cols: term.cols, rows: term.rows },
}));
});
term.onData((data) => {
ws.send(JSON.stringify({ type: "input", data }));
});
ws.addEventListener("message", (event) => {
const message = JSON.parse(event.data);
if (message.type === "output") {
term.write(message.data);
}
if (message.type === "ping") {
ws.send(JSON.stringify({
type: "pong",
data: { timestamp: message.timestamp ?? message.data },
}));
}
});
window.addEventListener("resize", () => {
fitAddon.fit();
ws.send(JSON.stringify({
type: "resize",
data: { cols: term.cols, rows: term.rows },
}));
});
</script>
</body>
</html>