-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
62 lines (58 loc) · 1.49 KB
/
app.js
File metadata and controls
62 lines (58 loc) · 1.49 KB
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
const express = require("express");
const http = require("http");
const socketio = require("socket.io");
const app = express();
const server = http.createServer(app);
const io = socketio(server);
const port = process.env.PORT || 4001;
let questions = [
{
id: 0,
question: "What is the square root of 4?",
claimed: false,
active: true,
},
{
id: 1,
question: "Why does this app exist?",
claimed: false,
active: false,
},
{
id: 2,
question: "How large is the expanding universe?",
claimed: false,
active: false,
},
];
io.on("connection", (socket) => {
console.log("New client connected");
socket.emit("questionUpdate", questions);
socket.on("questionClaim", (claimedID) => {
for (let i = 0; i < questions.length; i++) {
if (questions[i].id === claimedID.id) {
questions[i].claimed = claimedID.name;
io.sockets.emit("questionUpdate", questions);
break;
}
}
});
socket.on("move", action => {
for (let i = 0; i < questions.length; i++) {
if (questions[i].active) {
questions[i].active = false;
if (action === "back") {
questions[i-1].active = true;
} else if (action === "next") {
questions[i+1].active = true;
}
io.sockets.emit("questionUpdate", questions);
break;
}
}
})
socket.on("disconnect", () => {
console.log("Client disconnected");
});
});
server.listen(port, () => console.log(`Listening on port ${port}`));