| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | |
| 4 | <head> |
| 5 | <meta charset="UTF-8"> |
| 6 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 7 | <title>Glaze WebSocket + HTTP Server</title> |
| 8 | <style> |
| 9 | body { |
| 10 | font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; |
| 11 | margin: 0; |
| 12 | padding: 20px; |
| 13 | background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| 14 | min-height: 100vh; |
| 15 | } |
| 16 | |
| 17 | .container { |
| 18 | max-width: 900px; |
| 19 | margin: 0 auto; |
| 20 | background: white; |
| 21 | border-radius: 10px; |
| 22 | box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
| 23 | overflow: hidden; |
| 24 | } |
| 25 | |
| 26 | .header { |
| 27 | background: #2c3e50; |
| 28 | color: white; |
| 29 | padding: 20px; |
| 30 | text-align: center; |
| 31 | } |
| 32 | |
| 33 | .content { |
| 34 | padding: 20px; |
| 35 | } |
| 36 | |
| 37 | #messages { |
| 38 | height: 400px; |
| 39 | overflow-y: auto; |
| 40 | border: 2px solid #ecf0f1; |
| 41 | padding: 15px; |
| 42 | background: #f8f9fa; |
| 43 | margin: 20px 0; |
| 44 | border-radius: 5px; |
| 45 | font-family: 'Courier New', monospace; |
| 46 | font-size: 14px; |
| 47 | } |
| 48 | |
| 49 | .message { |
| 50 | margin: 8px 0; |
| 51 | padding: 8px; |
| 52 | border-radius: 4px; |
| 53 | } |
| 54 | |
| 55 | .system { |
| 56 | background: #e8f4fd; |
| 57 | color: #2980b9; |
| 58 | border-left: 4px solid #3498db; |
| 59 | } |
| 60 | |
| 61 | .user { |
| 62 | background: #e8f5e8; |
| 63 | color: #27ae60; |
| 64 | border-left: 4px solid #2ecc71; |
| 65 | } |
| 66 | |
| 67 | .server { |
| 68 | background: #fef9e7; |
| 69 | color: #f39c12; |
| 70 | border-left: 4px solid #f1c40f; |
| 71 | } |
| 72 | |
| 73 | .error { |
| 74 | background: #fadbd8; |
| 75 | color: #e74c3c; |
| 76 | border-left: 4px solid #e74c3c; |
| 77 | } |
| 78 | |
| 79 | .input-group { |
| 80 | display: flex; |
| 81 | gap: 10px; |
| 82 | margin: 20px 0; |
| 83 | } |
| 84 | |
| 85 | input[type="text"] { |
| 86 | flex: 1; |
| 87 | padding: 12px; |
| 88 | border: 2px solid #bdc3c7; |
| 89 | border-radius: 5px; |
| 90 | font-size: 16px; |
| 91 | } |
| 92 | |
| 93 | input[type="text"]:focus { |
| 94 | border-color: #3498db; |
| 95 | outline: none; |
| 96 | } |
| 97 | |
| 98 | button { |
| 99 | padding: 12px 20px; |
| 100 | background: #3498db; |
| 101 | color: white; |
| 102 | border: none; |
| 103 | border-radius: 5px; |
| 104 | cursor: pointer; |
| 105 | font-size: 16px; |
| 106 | transition: background 0.3s; |
| 107 | } |
| 108 | |
| 109 | button:hover { |
| 110 | background: #2980b9; |
| 111 | } |
| 112 | |
| 113 | button:disabled { |
| 114 | background: #bdc3c7; |
| 115 | cursor: not-allowed; |
| 116 | } |
| 117 | |
| 118 | .status { |
| 119 | padding: 10px; |
| 120 | border-radius: 5px; |
| 121 | margin: 10px 0; |
| 122 | font-weight: bold; |
| 123 | } |
| 124 | |
| 125 | .connected { |
| 126 | background: #d5f4e6; |
| 127 | color: #27ae60; |
| 128 | } |
| 129 | |
| 130 | .disconnected { |
| 131 | background: #fadbd8; |
| 132 | color: #e74c3c; |
| 133 | } |
| 134 | |
| 135 | .commands { |
| 136 | background: #f8f9fa; |
| 137 | padding: 15px; |
| 138 | border-radius: 5px; |
| 139 | margin: 20px 0; |
| 140 | } |
| 141 | |
| 142 | .commands h3 { |
| 143 | margin-top: 0; |
| 144 | } |
| 145 | |
| 146 | .commands code { |
| 147 | background: #e9ecef; |
| 148 | padding: 2px 6px; |
| 149 | border-radius: 3px; |
| 150 | font-family: 'Courier New', monospace; |
| 151 | } |
| 152 | </style> |
| 153 | </head> |
| 154 | |
| 155 | <body> |
| 156 | <div class="container"> |
| 157 | <div class="header"> |
| 158 | <h1>🚀 Glaze WebSocket + HTTP Server</h1> |
| 159 | <p>Real-time communication with header-only C++ WebSocket implementation</p> |
| 160 | </div> |
| 161 | |
| 162 | <div class="content"> |
| 163 | <div id="status" class="status disconnected">🔴 Disconnected</div> |
| 164 | |
| 165 | <div class="commands"> |
| 166 | <h3>Available Commands:</h3> |
| 167 | <ul> |
| 168 | <li><code>/ping</code> - Send a ping to the server</li> |
| 169 | <li><code>/clients</code> - Get number of connected clients</li> |
| 170 | <li><code>/echo [message]</code> - Echo a message back</li> |
| 171 | <li>Any other text will be broadcast to all clients</li> |
| 172 | </ul> |
| 173 | </div> |
| 174 | |
| 175 | <div id="messages"></div> |
| 176 | |
| 177 | <div class="input-group"> |
| 178 | <input id="messageInput" type="text" placeholder="Type your message here..." disabled /> |
| 179 | <button id="sendButton" onclick="sendMessage()" disabled>Send</button> |
| 180 | <button onclick="sendPing()">Ping</button> |
| 181 | <button onclick="getClientCount()">Count</button> |
| 182 | </div> |
| 183 | </div> |
| 184 | </div> |
| 185 | |
| 186 | <script> |
| 187 | let ws; |
| 188 | const messages = document.getElementById('messages'); |
| 189 | const input = document.getElementById('messageInput'); |
| 190 | const sendButton = document.getElementById('sendButton'); |
| 191 | const status = document.getElementById('status'); |
| 192 | |
| 193 | function addMessage(text, type = 'system') { |
| 194 | const div = document.createElement('div'); |
| 195 | div.className = 'message ' + type; |
| 196 | const timestamp = new Date().toLocaleTimeString(); |
| 197 | div.innerHTML = `<strong>${timestamp}</strong> - ${text}`; |
| 198 | messages.appendChild(div); |
| 199 | messages.scrollTop = messages.scrollHeight; |
| 200 | } |
| 201 | |
| 202 | function updateStatus(connected) { |
| 203 | if (connected) { |
| 204 | status.textContent = '🟢 Connected to WebSocket server'; |
| 205 | status.className = 'status connected'; |
| 206 | input.disabled = false; |
| 207 | sendButton.disabled = false; |
| 208 | } else { |
| 209 | status.textContent = '🔴 Disconnected from server'; |
| 210 | status.className = 'status disconnected'; |
| 211 | input.disabled = true; |
| 212 | sendButton.disabled = true; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | function connect() { |
| 217 | try { |
| 218 | addMessage('🔄 Connecting to WebSocket server...', 'system'); |
| 219 | ws = new WebSocket('ws://localhost:8080/ws'); |
| 220 | |
| 221 | ws.onopen = function () { |
| 222 | addMessage('✅ Connected successfully!', 'system'); |
| 223 | updateStatus(true); |
| 224 | }; |
| 225 | |
| 226 | ws.onmessage = function (event) { |
| 227 | if (event.data.startsWith('📢')) { |
| 228 | addMessage(event.data, 'system'); |
| 229 | } else if (event.data.startsWith('🔄') || event.data.startsWith('🏓') || event.data.startsWith('👥')) { |
| 230 | addMessage(event.data, 'server'); |
| 231 | } else { |
| 232 | addMessage(event.data, 'server'); |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | ws.onclose = function () { |
| 237 | addMessage('❌ Connection closed. Attempting to reconnect in 3 seconds...', 'system'); |
| 238 | updateStatus(false); |
| 239 | setTimeout(connect, 3000); |
| 240 | }; |
| 241 | |
| 242 | ws.onerror = function () { |
| 243 | addMessage('🚨 Connection error occurred', 'error'); |
| 244 | }; |
| 245 | |
| 246 | } catch (error) { |
| 247 | addMessage('❌ Failed to connect: ' + error.message, 'error'); |
| 248 | setTimeout(connect, 5000); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | function sendMessage() { |
| 253 | if (ws && ws.readyState === WebSocket.OPEN && input.value.trim()) { |
| 254 | const message = input.value.trim(); |
| 255 | ws.send(message); |
| 256 | addMessage('You: ' + message, 'user'); |
| 257 | input.value = ''; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | function sendPing() { |
| 262 | if (ws && ws.readyState === WebSocket.OPEN) { |
| 263 | ws.send('/ping'); |
| 264 | addMessage('You: /ping', 'user'); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | function getClientCount() { |
| 269 | if (ws && ws.readyState === WebSocket.OPEN) { |
| 270 | ws.send('/clients'); |
| 271 | addMessage('You: /clients', 'user'); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // Event listeners |
| 276 | input.addEventListener('keypress', function (e) { |
| 277 | if (e.key === 'Enter') { |
| 278 | sendMessage(); |
| 279 | } |
| 280 | }); |
| 281 | |
| 282 | // Auto-connect on page load |
| 283 | connect(); |
| 284 | </script> |
| 285 | </body> |
| 286 | |
| 287 | </html> |