-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (38 loc) · 1.24 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
38
39
40
41
42
43
const io = require('socket.io')();
const port = 8000;
let times = [];
let teamTimes = [];
io.on('connection', (client) => {
let addedUser = false;
// here you can start emitting events to the client
// subscribeToTimer is called by client whenever they update
client.on('subscribeToTimer', (time) => {
console.log('client is subscribing to timer with interval ', time.time);
client.username = time.username;
// teamTimes.push({
// username: 'name',
// time: 10
// });
let index = teamTimes.findIndex(element => element.username === client.username);
if (index !== -1){
teamTimes[index].time = time.time
} else {
teamTimes.push({
username: client.username,
time: time.time
})
}
times.push(time.time);
client.emit('times', teamTimes); // update all clients with updated times
});
client.on('addUser', (username) => {
if (addedUser) return;
client.username = username;
addedUser = true;
client.broadcast.emit('userOnline', {
username: client.username
})
})
});
io.listen(port);
console.log('listening on port ', port);