update node packages
[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 AssessmentModel = require("./models/assessment");
5 const ObjectId = require("bson-objectid");
6
7 module.exports = function(io)
8 {
9 io.of("/").on("connection", socket => { //student or monitor connexion
10 const aid = socket.handshake.query.aid;
11 const isTeacher = !!socket.handshake.query.secret && socket.handshake.query.secret == params.secret;
12
13 if (isTeacher)
14 {
15 socket.join(aid + "_teacher");
16 socket.on(message.allAnswers, m => { //send feedback to student (answers)
17 socket.broadcast.to(aid + "_student").emit(message.allAnswers, m);
18 });
19 }
20 else //student
21 {
22 const number = socket.handshake.query.number;
23 const password = socket.handshake.query.password;
24 AssessmentModel.checkPassword(ObjectId(aid), number, password, (err,ret) => {
25 if (!!err || !ret)
26 return; //wrong password, or some unexpected error...
27 AssessmentModel.newConnection(ObjectId(aid), number);
28 socket.broadcast.to(aid + "_teacher").emit(message.studentConnect, {number: number});
29 socket.join(aid + "_student");
30 socket.on(message.newAnswer, m => { //got answer from student client
31 socket.broadcast.to(aid + "_teacher").emit(message.newAnswer, m);
32 });
33 socket.on("disconnect", () => { //notify monitor + server
34 AssessmentEntity.setDiscoTime(ObjectId(aid), number);
35 socket.broadcast.to(aid + "_teacher").emit(message.studentDisconnect, {number: number});
36 });
37 });
38 }
39 });
40 }