Uwaa/HTTP.Example/www-static/websocket.js
2024-11-22 06:40:43 +00:00

25 lines
No EOL
501 B
JavaScript

function log(text) {
document.getElementById("log").innerText += text + "\n";
}
log("Connecting");
const ws = new WebSocket(`wss://${location.host}/`, "test");
function send(text) {
log(">> " + text);
ws.send(text);
}
ws.onerror = (e) => {
log("Error");
throw e;
};
ws.onopen = (event) => {
log("Connected");
send("Hello world");
};
ws.onclose = (event) => {
log("Closed");
};
ws.onmessage = (event) => {
log("<< " + event.data);
console.log(event.data);
};