-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (28 loc) · 881 Bytes
/
index.js
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
const WebSocket = require('ws');
const DataGenerator = require('./generateMockData.js');
const wss = new WebSocket.Server({port: 8080});
// Broadcast mock data to all connected clients.
const broadcastMockData = () =>
{
const mockData = DataGenerator.generateMockData();
wss.clients.forEach((client) =>
{
if (client.readyState === WebSocket.OPEN)
{
client.send(JSON.stringify(mockData));
}
});
};
// Set interval to generate and send mock data every n seconds.
setInterval(broadcastMockData, 5000);
wss.on('connection', (ws) =>
{
console.log('Client connected');
// Send initial mock data when a client connects
ws.send(JSON.stringify(DataGenerator.generateMockData()));
ws.on('close', () =>
{
console.log('Client disconnected');
});
});
console.log('WebSocket server is running.');