-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (37 loc) · 1.05 KB
/
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
36
37
38
39
40
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require('socket.io');
const io = new Server(server);
app.use(express.static('public'));
io.on('connection', (socket) => {
let percentComplete = new Array(10).fill(0);
let componentIndex = 0;
let interval;
function sendStatus() {
componentIndex =
componentIndex < percentComplete.length ? componentIndex : 0;
if (percentComplete[componentIndex] < 100) {
percentComplete[componentIndex] += 10;
} else {
componentIndex += 1;
}
if (componentIndex < percentComplete.length) {
const status = {
percent: percentComplete[componentIndex],
componentIndex: componentIndex,
};
socket.emit('status', status);
} else {
clearInterval(interval);
}
}
setTimeout(() => {
interval = setInterval(sendStatus, 200);
}, 2000);
});
const port = process.env.PORT || 80;
server.listen(port, () => {
console.log(`listening on *:${port}`);
});