Simplify draft monitoring + sockets logic
[qomet.git] / sockets.js
CommitLineData
e5ec7dea 1const message = require("./public/javascripts/utils/socketMessages");
e99c53fb 2const params = require("./config/parameters");
e5ec7dea
BA
3const AssessmentEntity = require("./entities/assessment");
4const ObjectId = require("bson-objectid");
e99c53fb 5
f6648c37
BA
6module.exports = function(io)
7{
8 io.of("/").on("connection", socket => {
9 const aid = socket.handshake.query.aid;
10 socket.join(aid);
11 // Student or monitor connexion
12 const isTeacher = !!socket.handshake.query.secret && socket.handshake.query.secret == params.secret;
13 if (isTeacher)
14 {
15 socket.on(message.newAnswer, m => { //got answer from student
16 socket.emit(message.newAnswer, m);
e5ec7dea 17 });
f6648c37
BA
18 socket.on(message.allAnswers, m => { //send feedback to student (answers)
19 if (!!students[m.number]) //TODO: namespace here... room quiz
20 socket.broadcast.to(aid).emit(message.allAnswers, m);
e5ec7dea 21 });
e99c53fb 22 }
f6648c37
BA
23 else //student
24 {
25 const number = socket.handshake.query.number;
26 const password = socket.handshake.query.password;
27 AssessmentEntity.checkPassword(ObjectId(aid), number, password, (err,ret) => {
28 if (!!err || !ret)
29 return; //wrong password, or some unexpected error...
30 // TODO: Prevent socket connection (just ignore) if student already connected
31// io.of('/').in(aid).clients((error, clients) => {
32// if (error)
33// throw error;
34// if (clients.some( c => { return c. .. == number; }))
35// // Problem: we just have a list of socket IDs (not handshakes)
36// });
37 // TODO: next is conditional to "student not already taking the exam"
38 socket.on(message.allAnswers, () => { //got all answers from teacher
39 socket.emit(message.allAnswers, m);
e99c53fb 40 });
f6648c37
BA
41 socket.on("disconnect", () => {
42 //TODO: notify monitor (grey low opacity background)
43 //Also send to server: discoTime in assessment.papers ...
e99c53fb 44 });
f6648c37
BA
45 });
46 }
e99c53fb
BA
47 });
48}