Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions websocket-server.js
Original file line number Diff line number Diff line change
@@ -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;