diff options
Diffstat (limited to 'testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.js')
-rw-r--r-- | testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.js b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.js new file mode 100644 index 0000000000..37d892a28b --- /dev/null +++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.js @@ -0,0 +1,26 @@ +window.addEventListener("DOMContentLoaded", () => { + const websocket = new WebSocket("ws://localhost:6789/"); + + document.querySelector(".minus").addEventListener("click", () => { + websocket.send(JSON.stringify({ action: "minus" })); + }); + + document.querySelector(".plus").addEventListener("click", () => { + websocket.send(JSON.stringify({ action: "plus" })); + }); + + websocket.onmessage = ({ data }) => { + const event = JSON.parse(data); + switch (event.type) { + case "value": + document.querySelector(".value").textContent = event.value; + break; + case "users": + const users = `${event.count} user${event.count == 1 ? "" : "s"}`; + document.querySelector(".users").textContent = users; + break; + default: + console.error("unsupported event", event); + } + }; +}); |