|
| 1 | +export const html = ({ name }) => /* html */ `<!doctype html> |
| 2 | +<head> |
| 3 | + <title>CrossWS Test Page</title> |
| 4 | +</head> |
| 5 | +<body> |
| 6 | + <h1>CrossWS Test Page (${name || "?"})</h1> |
| 7 | +
|
| 8 | + <button onclick="sendPing()">Send Ping</button> |
| 9 | + <button onclick="connect()">Reconnect</button> |
| 10 | + <button onclick="clearLogs()">Clear</button> |
| 11 | +
|
| 12 | + <pre id="logs"></pre> |
| 13 | +
|
| 14 | + <script type="module"> |
| 15 | + const url = "ws://" + location.host + "/_ws"; |
| 16 | +
|
| 17 | + const logsEl = document.querySelector("#logs"); |
| 18 | + let lastTime = performance.now(); |
| 19 | + const log = (...args) => { |
| 20 | + const now = performance.now(); |
| 21 | + const time = Math.round((now - lastTime) * 1000) / 1000; |
| 22 | + lastTime = now; |
| 23 | +
|
| 24 | + console.log("[ws]", ...args); |
| 25 | + logsEl.innerText += "\\n (" + String(time).padStart(4, ' ') + "ms) " + args.join(" "); |
| 26 | + }; |
| 27 | +
|
| 28 | + let ws |
| 29 | + globalThis.connect = async () => { |
| 30 | + if (ws) { |
| 31 | + log("Closing..."); |
| 32 | + ws.close(); |
| 33 | + } |
| 34 | +
|
| 35 | + log("Connecting to", url, "..."); |
| 36 | + ws = new WebSocket(url); |
| 37 | +
|
| 38 | + ws.addEventListener("message", (event) => { |
| 39 | + log("Message from server:", event.data); |
| 40 | + }); |
| 41 | +
|
| 42 | + log("Waiting for connection..."); |
| 43 | + await new Promise((resolve) => ws.addEventListener("open", resolve)); |
| 44 | + } |
| 45 | +
|
| 46 | + globalThis.clearLogs = () => { |
| 47 | + logsEl.innerText = '' |
| 48 | + } |
| 49 | +
|
| 50 | + globalThis.sendPing = () => { |
| 51 | + log("Sending ping..."); |
| 52 | + ws.send("ping"); |
| 53 | + } |
| 54 | +
|
| 55 | + await connect(); |
| 56 | + sendPing() |
| 57 | + </script> |
| 58 | +</body> |
| 59 | +`; |
0 commit comments