-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
37 lines (29 loc) · 1.07 KB
/
server.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
36
37
const express = require('express');
const http = require('http');
const {Server} = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.use(express.static('public'));
io.on('connection', (socket) => {
console.log("new client connected");
const sendSalesData = () => {
const salesData = generateRandomSalesData();
socket.emit('salesData', salesData);
};
const interval = setInterval(sendSalesData, 2000);
socket.on('disconnect', () => {
console.log('Client disconnected');
clearInterval(interval);
});
});
const generateRandomSalesData = () => {
return [
{product: 'Product A', sales: Math.floor(Math.random() * 100)},
{product: 'Product B', sales: Math.floor(Math.random() * 100)},
{product: 'Product C', sales: Math.floor(Math.random() * 100)},
{product: 'Product D', sales: Math.floor(Math.random() * 100)}
];
};
const PORT = process.env.PORT || 4000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));