-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
41 lines (31 loc) · 1023 Bytes
/
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
38
39
40
41
const express = require('express');
const path = require('path');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
app.use(express.static(path.join(__dirname, 'public')));
let timers = {};
io.on('connection', function(socket) {
socket.on('start', function(data) {
startTimer(data);
});
function startTimer(data) {
let timer = data.time;
let timerId = data.timerId;
timers[timerId] = setInterval(() => {
timer--;
io.sockets.emit('time', { time: timer, timerId: timerId });
if (timer <= 0) {
clearInterval(timers[timerId]);
delete timers[timerId];
io.sockets.emit('end', timerId);
}
}, 1000);
}
});
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/public/index.html'));
});
server.listen(3000, function() {
console.log('App is running on http://localhost:3000');
});