Fix monitoring presence (TODO: weird Vue syntax...)
[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(message.studentBlur, m => {
34 socket.broadcast.to(aid + "_teacher").emit(message.studentBlur, m);
35 });
36 socket.on(message.studentFocus, m => {
37 socket.broadcast.to(aid + "_teacher").emit(message.studentFocus, m);
38 });
39 socket.on(message.studentResize, m => {
40 socket.broadcast.to(aid + "_teacher").emit(message.studentResize, m);
41 });
42 socket.on(message.studentFullscreen, m => {
43 socket.broadcast.to(aid + "_teacher").emit(message.studentFullscreen, m);
44 });
45 socket.on("disconnect", () => { //notify monitor + server
46 AssessmentEntity.setDiscoTime(ObjectId(aid), number);
47 socket.broadcast.to(aid + "_teacher").emit(message.studentDisconnect, {number: number});
48 });
49 });
50 }
51 });
52 }