-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
71 lines (60 loc) · 1.68 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const express = require("express");
const app = express();
const http = require('http').Server(app);
const port = process.env.PORT || 4001;
const io = require('socket.io')(http);
const MLBApi = require('node-mlb-api')
let standings = [];
const getApiAndEmit = async (socket, force = false) => {
let oldStandings = standings;
standings = [];
// National League
let nlStandings = await MLBApi.getStandings('NL')
.then(res => {
res["records"].forEach(division => {
standings.push(division);
});
}).catch(err => console.error(err));
// American League
let alStandings = await MLBApi.getStandings('AL')
.then(res => {
res["records"].forEach(division => {
standings.push(division);
});
}).catch(err => console.error(err));
// emit iff standings have changed
if (JSON.stringify(oldStandings) != JSON.stringify(standings) || force){
console.log('emitting');
emit(socket, standings);
}
}
function emit(socket, data){
try {
socket.emit("FromAPI", data)
} catch (err) {
console.error(err);
}
}
// every 10s, check the standings and send to socket
let interval;
let refreshRate = 10000; // milliseconds
if (interval){
clearInterval(interval);
}
getApiAndEmit(io, true);
interval = setInterval(() => {
getApiAndEmit(io);
}, refreshRate);
// emit every time a user enters
io.on('connection', (socket) => {
console.log('a user connected');
getApiAndEmit(socket, true);
socket.on('disconnect', () => {
console.log('a user disconnected');
});
});
app.get('/', (req, res) => {
res.sendFile(__dirname + "/frontend/index.html");
});
app.use(express.static('frontend'));
http.listen(port, () => console.log(`listening on port ${port}`));