farpatch-ui/static/old/rtt.html

178 lines
4.4 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="xterm.css" />
<script src="xterm.js"></script>
<script src="xterm-addon-fit.js"></script>
<script src="jquery.js"></script>
</head>
<style>
body {
font-family: sans;
}
</style>
<body>
<div id="terminal" style="height: 90vh"></div>
<input type="text" id="line" style="width: 50%"></input>
<select id="eol">
<option value="cr">CR</option>
<option value="lf">LF</option>
<option value="crlf">CR/LF</option>
<option value="none">None</option>
<option value="hex">Hex</option>
</select>
<button id="sendbtn" type="button">SEND</button>
<input type="checkbox" id="echo" checked>Echo</input>
<button type="button" id="clear">CLEAR</button>
<br>
<span id="status">Connecting...</span>
<script>
var line_items = [];
var idx = -1;
function parseHexString(str) {
str = str.replace("0x", "");
str = str.replace(" ", "");
var result = [];
while (str.length >= 2) {
result.push(parseInt(str.substring(0, 2), 16));
str = str.substring(2, str.length);
}
return result;
}
function sendLine() {
var line = document.querySelector("#line").value;
var type = document.querySelector("#eol").value;
var echo = document.querySelector("#echo").checked;
if (!line_items.includes(line)) {
line_items.push(line)
idx = line_items.length - 1
}
if (type == "cr") line += "\r"
else if (type == "lf") line += "\n"
else if (type == "crlf") line += "\r\n"
else if (type == "hex") {
line = parseHexString(line)
}
if (echo)
term.write(line)
socket.send(line)
}
document.querySelector("#clear").onclick = event => {
term.clear();
}
document.querySelector("#sendbtn").onclick = event => {
sendLine();
}
document.querySelector("#line").addEventListener("keydown", event => {
if (event.key == "ArrowUp") {
console.log(idx)
if (line_items.length - 1 == idx) idx--;
event.srcElement.value = line_items[idx]
if (idx > 0) idx--;
}
if (event.key == "ArrowDown") {
console.log(idx)
if (idx < line_items.length - 1) {
idx++;
}
event.srcElement.value = line_items[idx]
}
if (event.key !== "Enter") return;
sendLine();
event.preventDefault(); // No need to `return false;`.
});
var term = new Terminal({ cursorBlink: true, convertEol: true });
var fitAddon = new FitAddon.FitAddon();
term.loadAddon(fitAddon);
var socket;
function createSocket() {
if (socket && !socket.replacementCreated) {
socket.close();
}
socket = new WebSocket("ws://" + window.location.host + "/ws/rtt");
socket.binaryType = 'arraybuffer';
socket.replacementCreated = false;
socket.ontimeout = function () {
if (socket.readyState == WebSocket.OPEN) {
socket.send('');
}
};
socket.onopen = function (event) {
$("#status").text("Connected");
if (firstConnection) {
term.write("\x1B[1;3;31m[Websocket] Connection established\x1B[0m\r\n");
} else {
term.write("[Websocket] Connection reestablished\n");
}
firstConnection = false;
};
socket.onmessage = function (event) {
term.write(new Uint8Array(event.data));
};
socket.onerror = function (event) {
socket.close();
}
socket.onclose = function (event) {
socket.onerror = undefined;
socket.onclose = undefined;
if (event.wasClean) {
$("#status").text("Connection closed");
} else {
$("#status").text("Reconnecting...");
}
clearInterval(socket.intervalId);
if (!socket.replacementCreated) {
socket.replacementCreated = true;
createSocket();
} else {
console.log("a replacement socket was already being created -- skipping");
}
};
socket.intervalId = setInterval(socket.ontimeout, 2000, socket);
}
createSocket();
term.open(document.getElementById('terminal'));
term.onData(chunk => {
socket.send(chunk)
})
fitAddon.activate(term)
fitAddon.fit()
term.focus()
window.addEventListener('resize', () => { fitAddon.fit() });
</script>
</body>
</html>