demo-app: rollback index.html
Kim committed
Mar 4, 2026 at 11:28 UTC
c0c335c7d69c243ce9398261018cdb23438ed30f
1 file changed
+54
-1
cmd/demo-app/static/index.html
+54
-1
@@ -17,6 +17,8 @@
17
<div class="toolbar">
18
<button id="httpPingBtn">HTTP Ping</button>
19
<button id="testCookiesBtn">Test Cookies</button>
20
+ <button id="wsConnectBtn">WS Connect</button>
21
+ <button id="wsSendBtn" disabled>WS Send "hello"</button>
22
</div>
23
24
<div class="canvas-wrapper">
@@ -30,6 +32,10 @@
32
const statusEl = document.getElementById('status');
33
const logEl = document.getElementById('log');
34
const httpPingBtn = document.getElementById('httpPingBtn');
35
+ const wsConnectBtn = document.getElementById('wsConnectBtn');
36
+ const wsSendBtn = document.getElementById('wsSendBtn');
37
+
38
+ let ws = null;
39
40
function log(message) {
41
const time = new Date().toISOString();
@@ -72,7 +78,54 @@
78
}
79
});
80
81
+ wsConnectBtn.addEventListener('click', () => {
82
+ if (ws && ws.readyState === WebSocket.OPEN) {
83
+ ws.close();
84
+ return;
85
+ }
86
+
87
+ const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
88
+ const basePath = location.pathname.endsWith('/') ? location.pathname : (location.pathname + '/');
89
+ const url = protocol + '//' + window.location.host + basePath + 'ws';
90
+
91
+ statusEl.textContent = 'WS: Connecting...';
92
+ log(`WS connecting to ${url}`);
93
+
94
+ ws = new WebSocket(url);
95
+
96
+ ws.onopen = () => {
97
+ statusEl.textContent = 'WS: Connected';
98
+ log('WS connected');
99
+ wsSendBtn.disabled = false;
100
+ wsConnectBtn.textContent = 'WS Disconnect';
101
+ };
102
+
103
+ ws.onclose = () => {
104
+ statusEl.textContent = 'WS: Disconnected';
105
+ log('WS disconnected');
106
+ wsSendBtn.disabled = true;
107
+ wsConnectBtn.textContent = 'WS Connect';
108
+ };
109
+
110
+ ws.onerror = (err) => {
111
+ log(`WS error: ${err.message || err}`);
112
+ };
113
+
114
+ ws.onmessage = (event) => {
115
+ log(`WS recv: ${event.data}`);
116
+ };
117
+ });
118
+
119
+ wsSendBtn.addEventListener('click', () => {
120
+ if (!ws || ws.readyState !== WebSocket.OPEN) {
121
+ log('WS send skipped: not connected');
122
+ return;
123
+ }
124
+ const msg = 'hello';
125
+ ws.send(msg);
126
+ log(`WS send: ${msg}`);
127
+ });
128
</script>
129
</body>
130
78
-</html>
131
+</html>
\ No newline at end of file