eeda127f53aca586ac52f8ce48cd0c142f8f7580
[qomet.git] / sockets.js
1 const message = require("./public/javascripts/utils/socketMessages");
2 const params = require("./config/parameters");
3 const AssessmentEntity = require("./entities/assessment");
4 const ObjectId = require("bson-objectid");
5
6 module.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);
17 });
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);
21 });
22 }
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);
40 });
41 socket.on("disconnect", () => {
42 //TODO: notify monitor (grey low opacity background)
43 //Also send to server: discoTime in assessment.papers ...
44 });
45 });
46 }
47 });
48 }