Debian Codegraph @

test/manual/ws.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>HTMX WebSocket Extension - Demo</title>
7 <script src="/dist/htmx.js"></script>
8 <script src="/src/ext/hx-ws.js"></script>
9 <style>
10 * {
11 margin: 0;
12 padding: 0;
13 box-sizing: border-box;
14 }
15
16 body {
17 font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
18 background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
19 min-height: 100vh;
20 padding: 2rem;
21 }
22
23 .container {
24 max-width: 1200px;
25 margin: 0 auto;
26 }
27
28 header {
29 text-align: center;
30 color: white;
31 margin-bottom: 3rem;
32 }
33
34 header h1 {
35 font-size: 3rem;
36 margin-bottom: 0.5rem;
37 text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
38 }
39
40 header p {
41 font-size: 1.2rem;
42 opacity: 0.9;
43 }
44
45 .demo-grid {
46 display: grid;
47 grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
48 gap: 2rem;
49 margin-bottom: 2rem;
50 }
51
52 .demo-card {
53 background: white;
54 border-radius: 12px;
55 padding: 1.5rem;
56 box-shadow: 0 10px 30px rgba(0,0,0,0.2);
57 transition: transform 0.2s;
58 }
59
60 .demo-card:hover {
61 transform: translateY(-4px);
62 }
63
64 .demo-card h2 {
65 color: #667eea;
66 margin-bottom: 1rem;
67 font-size: 1.5rem;
68 display: flex;
69 align-items: center;
70 gap: 0.5rem;
71 }
72
73 .status-indicator {
74 width: 12px;
75 height: 12px;
76 border-radius: 50%;
77 background: #ccc;
78 display: inline-block;
79 animation: pulse 2s ease-in-out infinite;
80 }
81
82 .status-indicator.connected {
83 background: #10b981;
84 }
85
86 @keyframes pulse {
87 0%, 100% { opacity: 1; }
88 50% { opacity: 0.5; }
89 }
90
91 .chat-container {
92 height: 300px;
93 border: 2px solid #e5e7eb;
94 border-radius: 8px;
95 overflow-y: auto;
96 margin-bottom: 1rem;
97 padding: 1rem;
98 background: #f9fafb;
99 scroll-behavior: smooth;
100 }
101
102 .notifications-container {
103 max-height: 300px;
104 overflow-y: auto;
105 scroll-behavior: smooth;
106 }
107
108 .message {
109 padding: 0.75rem;
110 margin-bottom: 0.5rem;
111 border-radius: 8px;
112 animation: slideIn 0.3s ease-out;
113 }
114
115 @keyframes slideIn {
116 from {
117 opacity: 0;
118 transform: translateX(-20px);
119 }
120 to {
121 opacity: 1;
122 transform: translateX(0);
123 }
124 }
125
126 .message.sent {
127 background: #dbeafe;
128 text-align: right;
129 margin-left: 20%;
130 }
131
132 .message.received {
133 background: #f3e8ff;
134 margin-right: 20%;
135 }
136
137 .message-time {
138 font-size: 0.75rem;
139 opacity: 0.6;
140 margin-top: 0.25rem;
141 }
142
143 input[type="text"] {
144 width: 100%;
145 padding: 0.75rem;
146 border: 2px solid #e5e7eb;
147 border-radius: 8px;
148 font-size: 1rem;
149 margin-bottom: 0.5rem;
150 transition: border-color 0.2s;
151 }
152
153 input[type="text"]:focus {
154 outline: none;
155 border-color: #667eea;
156 }
157
158 button {
159 width: 100%;
160 padding: 0.75rem;
161 background: #667eea;
162 color: white;
163 border: none;
164 border-radius: 8px;
165 font-size: 1rem;
166 font-weight: 600;
167 cursor: pointer;
168 transition: background 0.2s;
169 }
170
171 button:hover {
172 background: #5568d3;
173 }
174
175 button:active {
176 transform: scale(0.98);
177 }
178
179 .notification {
180 padding: 1rem;
181 border-left: 4px solid #667eea;
182 background: #f0f4ff;
183 border-radius: 4px;
184 margin-bottom: 0.75rem;
185 animation: slideIn 0.3s ease-out;
186 }
187
188 .notification-time {
189 font-size: 0.75rem;
190 opacity: 0.6;
191 margin-top: 0.25rem;
192 }
193
194 .counter {
195 font-size: 3rem;
196 font-weight: bold;
197 text-align: center;
198 color: #667eea;
199 margin: 1rem 0;
200 }
201
202 .ticker-item {
203 padding: 0.75rem;
204 border-bottom: 1px solid #e5e7eb;
205 display: flex;
206 justify-content: space-between;
207 align-items: center;
208 }
209
210 .ticker-symbol {
211 font-weight: bold;
212 color: #667eea;
213 }
214
215 .ticker-price {
216 font-size: 1.1rem;
217 }
218
219 .ticker-change {
220 padding: 0.25rem 0.5rem;
221 border-radius: 4px;
222 font-size: 0.875rem;
223 }
224
225 .ticker-change.up {
226 background: #d1fae5;
227 color: #065f46;
228 }
229
230 .ticker-change.down {
231 background: #fee2e2;
232 color: #991b1b;
233 }
234
235 .log-entry {
236 font-family: 'Courier New', monospace;
237 font-size: 0.875rem;
238 padding: 0.5rem;
239 border-bottom: 1px solid #e5e7eb;
240 }
241
242 .log-time {
243 color: #6b7280;
244 }
245
246 .log-event {
247 color: #667eea;
248 font-weight: bold;
249 }
250
251 .controls {
252 display: flex;
253 gap: 0.5rem;
254 margin-bottom: 1rem;
255 }
256
257 .controls button {
258 flex: 1;
259 padding: 0.5rem;
260 font-size: 0.875rem;
261 }
262
263 footer {
264 text-align: center;
265 color: white;
266 margin-top: 3rem;
267 padding-top: 2rem;
268 border-top: 1px solid rgba(255,255,255,0.2);
269 }
270
271 footer a {
272 color: white;
273 text-decoration: underline;
274 }
275 </style>
276 </head>
277 <body>
278 <div class="container">
279 <header>
280 <h1>🚀 HTMX WebSocket Extension</h1>
281 <p>Real-time bidirectional communication made simple</p>
282 </header>
283
284 <div class="demo-grid">
285 <!-- Chat Demo -->
286 <div class="demo-card" hx-ws:connect="/chat" hx-trigger="load" hx-target="#chat" hx-swap="beforeend">
287 <h2>
288 <span class="status-indicator connected"></span>
289 Live Chat
290 </h2>
291 <div id="chat" class="chat-container"></div>
292 <form hx-ws:send hx-trigger="submit" hx-target="#chat" hx-swap="beforeend">
293 <input type="text" name="message" placeholder="Type a message..." autocomplete="off" required>
294 <button type="submit">Send Message</button>
295 </form>
296 </div>
297
298 <!-- Notifications -->
299 <div class="demo-card" hx-ws:connect="/notifications" hx-trigger="load" hx-target="#notifications" hx-swap="afterbegin">
300 <h2>
301 <span class="status-indicator connected"></span>
302 Live Notifications
303 </h2>
304 <div id="notifications" class="notifications-container"></div>
305 </div>
306
307 <!-- Counter -->
308 <div class="demo-card" hx-ws:connect="/counter" hx-trigger="load">
309 <h2>
310 <span class="status-indicator connected"></span>
311 Shared Counter
312 </h2>
313 <div class="counter" id="counter">0</div>
314 <div class="controls">
315 <button hx-ws:send hx-vals='{"action":"increment"}' hx-trigger="click">âž• Increment</button>
316 <button hx-ws:send hx-vals='{"action":"decrement"}' hx-trigger="click">âž– Decrement</button>
317 <button hx-ws:send hx-vals='{"action":"reset"}' hx-trigger="click">🔄 Reset</button>
318 </div>
319 </div>
320
321 <!-- Stock Ticker -->
322 <div class="demo-card" hx-ws:connect="/ticker" hx-trigger="load">
323 <h2>
324 <span class="status-indicator connected"></span>
325 Stock Ticker
326 </h2>
327 <div id="ticker"></div>
328 </div>
329
330 <!-- Dashboard -->
331 <div class="demo-card" hx-ws:connect="/dashboard" hx-trigger="load">
332 <h2>
333 <span class="status-indicator connected"></span>
334 System Dashboard
335 </h2>
336 <div id="cpu">CPU: 0%</div>
337 <div id="memory">Memory: 0%</div>
338 <div id="disk">Disk: 0%</div>
339 </div>
340
341 <!-- Event Log -->
342 <div class="demo-card">
343 <h2>
344 <span class="status-indicator"></span>
345 Event Log
346 </h2>
347 <div id="event-log" style="max-height: 300px; overflow-y: auto; font-size: 0.875rem;">
348 <div class="log-entry">
349 <span class="log-time">00:00:00</span>
350 <span class="log-event">SYSTEM</span>
351 Extension loaded
352 </div>
353 </div>
354 </div>
355 </div>
356
357 <footer>
358 <p>
359 <strong>Demo Instructions:</strong> Start the WebSocket server with
360 <code>node test/manual/ws-server.js</code>
361 </p>
362 <p>
363 Learn more: <a href="https://htmx.org/extensions/websockets/">htmx.org/extensions/websockets/</a>
364 </p>
365 </footer>
366 </div>
367
368 <script>
369 console.log('HTMX loaded:', typeof htmx !== 'undefined');
370 console.log('WS extension loaded:', typeof htmx !== 'undefined' && htmx.ext && htmx.ext.ws);
371
372 // Event logging
373 function logEvent(type, detail) {
374 const log = document.getElementById('event-log');
375 const entry = document.createElement('div');
376 entry.className = 'log-entry';
377 const time = new Date().toLocaleTimeString();
378 entry.innerHTML = `<span class="log-time">${time}</span> <span class="log-event">${type}</span> ${detail}`;
379 log.insertBefore(entry, log.firstChild);
380
381 // Keep only last 20 entries
382 while (log.children.length > 20) {
383 log.removeChild(log.lastChild);
384 }
385 }
386
387 // Listen to WebSocket events
388 document.addEventListener('htmx:before:ws:connection', (e) => {
389 let attempt = e.detail.connection.attempt;
390 if (attempt === 0) {
391 logEvent('CONNECT', `Connecting to ${e.detail.connection.url}`);
392 } else {
393 logEvent('RECONNECT', `Attempting reconnection (attempt ${attempt})`);
394 }
395 });
396
397 document.addEventListener('htmx:after:ws:connection', (e) => {
398 logEvent('CONNECTED', `Connected to ${e.detail.url}`);
399 });
400
401 document.addEventListener('htmx:ws:close', (e) => {
402 logEvent('CLOSE', `Connection closed: ${e.detail.url}`);
403 });
404
405 document.addEventListener('htmx:ws:error', (e) => {
406 logEvent('ERROR', `WebSocket error: ${e.detail.url}`);
407 });
408
409 document.addEventListener('htmx:before:ws:send', (e) => {
410 logEvent('SEND', `Sending message`);
411 });
412
413 document.addEventListener('htmx:after:ws:message', (e) => {
414 logEvent('MESSAGE', `Received message`);
415 });
416
417 // Clear chat input after sending
418 document.addEventListener('htmx:after:ws:send', (e) => {
419 if (e.target.matches('form')) {
420 const input = e.target.querySelector('input[name="message"]');
421 if (input) {
422 input.value = '';
423 }
424 }
425 });
426
427 // Auto-scroll chat and notifications after new content is swapped in
428 document.body.addEventListener('htmx:afterSwap', (e) => {
429 // Scroll chat to bottom when new messages arrive
430 if (e.target.id === 'chat' || e.target.closest('#chat')) {
431 const chatContainer = document.getElementById('chat');
432 if (chatContainer) {
433 setTimeout(() => {
434 chatContainer.scrollTop = chatContainer.scrollHeight;
435 }, 10);
436 }
437 }
438
439 // Scroll notifications to top when new notifications arrive (afterbegin = newest at top)
440 if (e.target.id === 'notifications' || e.target.closest('#notifications')) {
441 const notificationsContainer = document.getElementById('notifications');
442 if (notificationsContainer) {
443 setTimeout(() => {
444 notificationsContainer.scrollTop = 0;
445 }, 10);
446 }
447 }
448 });
449 </script>
450 </body>
451 </html>