checkpoint 2: websocket echo + test page
This commit is contained in:
1 parent
2c783e07ca
commit
8cb4c3556b
2 files changed
+49
-9
No files matched your search
@@ -0,0 +1,24 @@
|
||||
<!doctype html>
|
||||
<meta charset="utf-8" />
|
||||
<title>WebSocket test</title>
|
||||
<button id="send">Send</button>
|
||||
<pre id="log"></pre>
|
||||
|
||||
<script>
|
||||
const logEl = document.getElementById("log");
|
||||
const log = (msg) => (logEl.textContent += msg + "\n");
|
||||
const wsProto = location.protocol === "https:" ? "wss:" : "ws:";
|
||||
const ws = new WebSocket(`${wsProto}//${location.host}/ws`);
|
||||
|
||||
ws.onopen = () => {
|
||||
log("open");
|
||||
ws.send(JSON.stringify({ type: "ECHO", text: "hello" }));
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => log("recv: " + event.data);
|
||||
ws.onclose = (event) => log(`close ${event.code}`);
|
||||
|
||||
document.getElementById("send").onclick = () => {
|
||||
ws.send(JSON.stringify({ type: "ECHO", text: "button " }));
|
||||
};
|
||||
</script>
|
||||
Reference in new issue
Block a user