Wealth / static /js /socket.js
EricaLuvGemma's picture
Create static/js/socket.js
febab1f verified
Raw
History Blame Contribute Delete
1.04 kB
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;
}