-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
175 lines (147 loc) · 4.66 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var argv = require("yargs").argv;
var auth = require("./auth");
var quizcodes = [];
if (argv.username && argv.password) {
auth.addAdmin(argv.username, argv.password);
} else {
console.log(
"#############################################################"
);
console.log(
"# Authentication for /quizmaster page disabled! #"
);
console.log(
"# Invoke with --username=user --password=pass if undesired! #"
);
console.log(
"#############################################################"
);
auth.setAuthEnabled(false);
}
// determine if minified JavaScript resources are served
var minjs = ".min";
if (argv.debug) {
console.log("### Serving non-minified JavaScript libraries!");
minjs = "";
}
// determine port to be used, the default is port 3000
var port = 3000;
if (argv.port) {
port = argv.port;
}
http.listen(port, function () {
console.log("listening on *:" + port);
});
app.use(express.static("static"));
app.get("/", function (req, res) {
res.sendFile(__dirname + "/resources/client.html");
});
app.get("/quizmaster", auth.handleRequest, function (req, res) {
res.sendFile(__dirname + "/resources/quizmaster.html");
});
app.get("/editor", function (req, res) {
res.sendFile(__dirname + "/resources/editor.html");
});
app.get("/singleplayer", function (req, res) {
res.sendFile(__dirname + "/resources/singleplayer.html");
});
app.get("/jquery.js", function (req, res) {
res.sendFile(__dirname + "/resources/jquery-1.11.1" + minjs + ".js");
});
app.get("/i18next.js", function (req, res) {
res.sendFile(__dirname + "/resources/i18next-1.9.0" + minjs + ".js");
});
io.on("connection", function (socket) {
console.log("a user connected");
socket.on("JoinRoom", function (rooms) {
if (!rooms || !rooms.length) return;
console.log("client joins room(s) " + rooms);
for (var i = 0; i < rooms.length; ++i) {
socket.join(rooms[i]);
}
});
// deliver discovery requests from clients to quizmasters
socket.on("DiscoveryRequest", function (request) {
if (!request || !request.code) return;
var request = {
code: request.code,
clientid: socket.id,
};
io.in("quizmaster").emit("DiscoveryRequest", request);
});
// deliver discovery responses from quizmasters to client
socket.on("DiscoveryResponse", function (response) {
if (!response) return;
io.in(response.recipient).emit("DiscoveryResponse", response);
});
// distribute questions to quiz clients
socket.on("QuizQuestion", function (questionEnvelope) {
if (
!questionEnvelope ||
!questionEnvelope.question ||
!questionEnvelope.quizinstance
)
return;
var question = questionEnvelope.question;
var quizinstance = questionEnvelope.quizinstance;
if (questionEnvelope.recipient) {
// if question is targeted at specific client, deliver there
io.in(questionEnvelope.recipient).emit("QuizQuestion", question);
} else {
// otherwise deliver to all clients of this quiz instance
//console.log(quizinstance + ": " + question.text);
io.in("clients." + quizinstance).emit("QuizQuestion", question);
}
});
socket.on("RequestQuestion", function (request) {
if (!request || !request.quizinstance) return;
console.log("question requested, quizinstance " + request.quizinstance);
request.clientid = socket.id;
io.in("quizmaster." + request.quizinstance).emit(
"RequestQuestion",
request
);
});
// forward answers to quiz master
socket.on("QuizAnswer", function (answer) {
if (!answer || !answer.quizinstance) return;
// determine number of clients for this quiz
var room = io.sockets.adapter.rooms["clients." + answer.quizinstance];
answer.totalClients = room ? Object.keys(room).length : 1;
io.in("quizmaster." + answer.quizinstance).emit("QuizAnswer", answer);
});
// forward correct option to clients
socket.on("RevealCorrect", function (revealMsg) {
if (!revealMsg || !revealMsg.quizinstance) return;
io.in("clients." + revealMsg.quizinstance).emit(
"RevealCorrect",
revealMsg.answers
);
});
socket.on("RequestQuizCode", function (request) {
var code = 1337;
do {
code = Math.floor(Math.random() * 100000);
} while (quizcodes.includes(code));
quizcodes.unshift(code);
while (quizcodes.length > 100) {
quizcodes.pop();
}
socket.emit("RequestQuizCodeResponse", { quizcode: "" + code });
});
socket.on("pong", function (msg) {
//console.log("Pong received from client");
});
socket.on("disconnect", function () {
console.log("user disconnected");
});
});
function sendHeartbeat() {
setTimeout(sendHeartbeat, 9999);
io.sockets.emit("ping", "Ping!");
}
setTimeout(sendHeartbeat, 9999);