File size: 1,037 Bytes
febab1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const output = document.getElementById("output");
const input = document.getElementById("command");
const sendButton = document.getElementById("send");

const protocol = location.protocol === "https:" ? "wss" : "ws";

const socket = new WebSocket(`${protocol}://${location.host}/ws`);

socket.onopen = () => {
    appendOutput("[Connected]\n");
};

socket.onclose = () => {
    appendOutput("\n[Disconnected]\n");
};

socket.onerror = () => {
    appendOutput("\n[Connection Error]\n");
};

socket.onmessage = (event) => {
    appendOutput(event.data);
};

function sendCommand() {

    const command = input.value;

    if (!command.trim()) return;

    socket.send(command);

    input.value = "";

    autoResize();
}

sendButton.addEventListener("click", sendCommand);

input.addEventListener("keydown", (e) => {

    if (e.key === "Enter" && !e.shiftKey) {

        e.preventDefault();

        sendCommand();
    }

});

function appendOutput(text) {

    output.textContent += text;

    output.scrollTop = output.scrollHeight;

}