2ce8ae9ad7f352b33c761ddcacd501282a808723
[qomet.git] / sockets.js
1 const message = require("./public/javascripts/utils/socketMessages");
2 const params = require("./config/parameters");
3 const AssessmentModel = require("./models/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
14 if (isTeacher)
15 {
16 socket.on(message.newAnswer, m => { //got answer from student
17 socket.emit(message.newAnswer, m);
18 });
19 socket.on(message.allAnswers, m => { //send feedback to student (answers)
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 AssessmentModel.checkPassword(ObjectId(aid), number, password, (err,ret) => {
28 if (!!err || !ret)
29 return; //wrong password, or some unexpected error...
30 socket.on("disconnect", () => {
31 //TODO: notify monitor (grey low opacity background)
32 //Also send to server: discoTime in assessment.papers ...
33 });
34 });
35 }
36 });
37 }