|
| 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>NYSE</title> |
| 7 | + <style> |
| 8 | + body { font-family: Arial, sans-serif; max-width: 600px; margin: 20px auto; } |
| 9 | + #deals { list-style: none; padding: 0; } |
| 10 | + #deals li { border: 1px solid #ddd; margin: 10px 0; padding: 10px; } |
| 11 | + form { margin-bottom: 20px; } |
| 12 | + </style> |
| 13 | +</head> |
| 14 | +<body> |
| 15 | + <h1>Test WebSocket Deals</h1> |
| 16 | + <form id="sub-form"> |
| 17 | + <label for="tags">Subscribe to Tags (comma-separated IDs):</label><br> |
| 18 | + <input type="text" id="tags" placeholder="e.g., 1,2,3" required> |
| 19 | + <button type="submit">Subscribe</button> |
| 20 | + </form> |
| 21 | + <h2>Received Deals:</h2> |
| 22 | + <ul id="deals"></ul> |
| 23 | + |
| 24 | + <script> |
| 25 | + let ws = null; |
| 26 | + const dealsList = document.getElementById('deals'); |
| 27 | + const subForm = document.getElementById('sub-form'); |
| 28 | + |
| 29 | + function connect() { |
| 30 | + ws = new WebSocket('ws://localhost:8000/ws'); |
| 31 | + |
| 32 | + ws.onopen = () => { |
| 33 | + console.log('Connected to WebSocket'); |
| 34 | + }; |
| 35 | + |
| 36 | + ws.onmessage = (event) => { |
| 37 | + const batch = JSON.parse(event.data); |
| 38 | + batch.forEach(deal => { |
| 39 | + const li = document.createElement('li'); |
| 40 | + li.innerHTML = `Tag: ${deal.tag_id} | Details: ${deal.details} (ID: ${deal.id})`; |
| 41 | + dealsList.appendChild(li); |
| 42 | + }); |
| 43 | + }; |
| 44 | + |
| 45 | + ws.onclose = () => { |
| 46 | + console.log('Disconnected. Reconnecting...'); |
| 47 | + setTimeout(connect, 1000); |
| 48 | + }; |
| 49 | + |
| 50 | + ws.onerror = (error) => { |
| 51 | + console.error('WebSocket error:', error); |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + subForm.addEventListener('submit', (e) => { |
| 56 | + e.preventDefault(); |
| 57 | + const tagsInput = document.getElementById('tags').value.trim(); |
| 58 | + if (tagsInput && ws && ws.readyState === WebSocket.OPEN) { |
| 59 | + const tags = tagsInput.split(',').map(t => parseInt(t.trim(), 10)).filter(t => !isNaN(t)); |
| 60 | + ws.send(JSON.stringify({ action: 'subscribe', tags })); |
| 61 | + console.log('Subscribed to tags:', tags); |
| 62 | + } else { |
| 63 | + alert('Connect first or enter valid tags!'); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + connect(); |
| 68 | + </script> |
| 69 | +</body> |
| 70 | +</html> |
0 commit comments