-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.html
36 lines (30 loc) · 884 Bytes
/
client.html
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
<!doctype html>
<div id="output"></div>
<script>
const url = new URL(location.href);
const channel = url.searchParams.get("channel");
const port = url.searchParams.get("port") || 7070;
const wsUri = `ws://localhost:${port}/`;
const output = document.querySelector("#output");
const websocket = new WebSocket(wsUri);
function writeToScreen(message) {
output.insertAdjacentHTML("beforeend", `<p>${message}</p>`);
}
function sendMessage(message) {
writeToScreen(`SENT: ${message}`);
websocket.send(message);
}
websocket.onopen = (e) => {
writeToScreen(`CONNECTED: ${wsUri}`);
sendMessage(channel);
};
websocket.onmessage = (e) => {
writeToScreen(`RECEIVED: ${e.data}`);
};
websocket.onclose = (e) => {
writeToScreen("DISCONNECTED");
};
websocket.onerror = (e) => {
writeToScreen(`ERROR: ${e.data}`);
};
</script>