diff --git a/websocket-server.js b/websocket-server.js new file mode 100644 index 0000000..47c05d5 --- /dev/null +++ b/websocket-server.js @@ -0,0 +1,38 @@ +/** + * WebSocket server for real-time crowd density simulation + * Pushes mock data every 2 seconds to connected clients + */ +const WebSocket = require('ws'); + +const wss = new WebSocket.Server({ port: 8080 }); + +function generateCrowdData() { + const zones = [ + { id: 'zone-a', name: 'Main Entrance', density: Math.floor(Math.random() * 100) }, + { id: 'zone-b', name: 'Hall B', density: Math.floor(Math.random() * 100) }, + { id: 'zone-c', name: 'Food Court', density: Math.floor(Math.random() * 100) }, + { id: 'zone-d', name: 'Emergency Exit', density: Math.floor(Math.random() * 20) }, + ].map(z => ({ + ...z, + status: z.density > 80 ? 'critical' : z.density > 60 ? 'warning' : 'normal', + })); + + return { timestamp: new Date().toISOString(), zones }; +} + +wss.on('connection', (ws) => { + console.log('Client connected'); + const interval = setInterval(() => { + if (ws.readyState === WebSocket.OPEN) { + ws.send(JSON.stringify(generateCrowdData())); + } + }, 2000); + + ws.on('close', () => { + clearInterval(interval); + console.log('Client disconnected'); + }); +}); + +console.log('WebSocket server running on ws://localhost:8080'); +module.exports = wss;