Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | import http from 'http'; import createObjectRouter from '../src/api/router.js'; // Create router with WebSocket support using object-based routing const router = createObjectRouter({ '/': { get: { handler: (req, res) => { res.writeHead(200, { 'Content-Type': 'text/html', 'Content-Security-Policy': "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline';" }); res.end(`<!DOCTYPE html> <html> <head> <title>WebSocket Object Routing Demo</title> <style> body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; } .container { margin: 20px 0; } #messages { border: 1px solid #ccc; height: 300px; overflow-y: scroll; padding: 10px; } .message { margin: 5px 0; padding: 5px; background: #f5f5f5; } input, button { margin: 5px; padding: 8px; } button { background: #007cba; color: white; border: none; cursor: pointer; } button:hover { background: #005a87; } </style> </head> <body> <h1>WebSocket Object Routing Demo</h1> <p>This demonstrates object-based routing with proper CSP headers.</p> <div class="container"> <h2>Chat Room</h2> <div id="messages"></div> <input type="text" id="messageInput" placeholder="Type a message..." /> <button id="sendBtn">Send</button> <button id="connectChatBtn">Connect to Chat</button> <button id="disconnectChatBtn">Disconnect</button> </div> <div class="container"> <h2>Real-time Notifications</h2> <div id="notifications"></div> <button id="connectNotificationsBtn">Connect to Notifications</button> <button id="disconnectNotificationsBtn">Disconnect</button> </div> <script> let chatWs = null; let notificationWs = null; function addMessage(containerId, message) { const container = document.getElementById(containerId); const div = document.createElement('div'); div.className = 'message'; div.textContent = new Date().toLocaleTimeString() + ': ' + message; container.appendChild(div); container.scrollTop = container.scrollHeight; } function connectChat() { if (chatWs) return; chatWs = new WebSocket('ws://localhost:3004/ws/chat'); chatWs.onopen = () => addMessage('messages', 'Connected to chat'); chatWs.onmessage = (event) => addMessage('messages', 'Received: ' + event.data); chatWs.onclose = () => { addMessage('messages', 'Disconnected from chat'); chatWs = null; }; } function disconnectChat() { if (chatWs) { chatWs.close(); chatWs = null; } } function sendMessage() { const input = document.getElementById('messageInput'); if (chatWs && input.value) { chatWs.send(input.value); addMessage('messages', 'Sent: ' + input.value); input.value = ''; } } function connectNotifications() { if (notificationWs) return; notificationWs = new WebSocket('ws://localhost:3004/ws/notifications'); notificationWs.onopen = () => addMessage('notifications', 'Connected to notifications'); notificationWs.onmessage = (event) => addMessage('notifications', 'Notification: ' + event.data); notificationWs.onclose = () => { addMessage('notifications', 'Disconnected from notifications'); notificationWs = null; }; } function disconnectNotifications() { if (notificationWs) { notificationWs.close(); notificationWs = null; } } // Event listeners document.getElementById('sendBtn').addEventListener('click', sendMessage); document.getElementById('connectChatBtn').addEventListener('click', connectChat); document.getElementById('disconnectChatBtn').addEventListener('click', disconnectChat); document.getElementById('connectNotificationsBtn').addEventListener('click', connectNotifications); document.getElementById('disconnectNotificationsBtn').addEventListener('click', disconnectNotifications); // Enter key support document.getElementById('messageInput').addEventListener('keypress', (e) => { if (e.key === 'Enter') { sendMessage(); } }); // Auto-connect on page load setTimeout(() => { connectChat(); connectNotifications(); }, 1000); </script> </body> </html>`); } } }, 'admin': { 'metrics': { get: { handler: (req, res) => { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(router.metrics, null, 2)); } } }, 'ws': { 'connections': { get: { handler: (req, res) => { const connections = router.getWebSocketConnections(); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ total: connections.length, connections: connections }, null, 2)); } } } } } }, { enableWebSockets: true, enableMetrics: true, enableVersioning: true }); // WebSocket routes router.addWebSocketRoute('/ws/chat', (ws) => { console.log('New chat connection established'); ws.send('Welcome to the chat room!'); ws.onmessage = (event) => { const message = event.data; console.log('Chat message received:', message); // Broadcast to all chat connections router.broadcast('/ws/chat', `User says: ${message}`); }; ws.socket.on('close', () => { console.log('Chat connection closed'); }); }); router.addWebSocketRoute('/ws/notifications', (ws) => { console.log('New notification connection established'); ws.send('Connected to notifications'); // Send periodic notifications const interval = setInterval(() => { if (ws.readyState === 1) { ws.send(`Notification at ${new Date().toLocaleTimeString()}`); } else { clearInterval(interval); } }, 5000); ws.socket.on('close', () => { console.log('Notification connection closed'); clearInterval(interval); }); }); // Create HTTP server const server = http.createServer((req, res) => { router.handle(req, res); }); // Handle WebSocket upgrade server.on('upgrade', (request, socket, head) => { router.handleWebSocketUpgrade(request, socket, head); }); const PORT = 3004; server.listen(PORT, () => { console.log(`WebSocket object routing demo server running on http://localhost:${PORT}`); console.log('Available endpoints:'); console.log(' GET / - Demo page'); console.log(' WS /ws/chat - Chat room'); console.log(' WS /ws/notifications - Real-time notifications'); console.log(' GET /admin/ws/connections - List active connections'); console.log(' GET /admin/metrics - Performance metrics'); }); |