dafe6d961773e523c98620c460eb26982b02999b
[qomet.git] / sockets.js
1 const message = require("./public/javascripts/utils/socketMessages");
2 const params = require("./config/parameters");
3 const EvaluationModel = require("./models/evaluation");
4 const ObjectId = require("bson-objectid");
5
6 module.exports = function(io)
7 {
8 io.of("/").on("connection", socket => { //student or monitor connexion
9 const aid = socket.handshake.query.aid;
10 const isTeacher = !!socket.handshake.query.secret && socket.handshake.query.secret == params.secret;
11
12 if (isTeacher)
13 {
14 socket.join(aid + "_teacher");
15 socket.on(message.allAnswers, m => { //send feedback to student (answers)
16 socket.broadcast.to(aid + "_student").emit(message.allAnswers, m);
17 });
18 }
19 else //student
20 {
21 const number = socket.handshake.query.number;
22 const password = socket.handshake.query.password;
23 EvaluationModel.checkPassword(ObjectId(aid), number, password, (err,ret) => {
24 if (!!err || !ret)
25 return; //wrong password, or some unexpected error...
26 EvaluationModel.newConnection(ObjectId(aid), number);
27 socket.broadcast.to(aid + "_teacher").emit(message.studentConnect, {number: number});
28 socket.join(aid + "_student");
29 socket.on(message.newAnswer, m => { //got answer from student client
30 socket.broadcast.to(aid + "_teacher").emit(message.newAnswer, m);
31 });
32 socket.on(message.studentBlur, m => {
33 socket.broadcast.to(aid + "_teacher").emit(message.studentBlur, m);
34 });
35 socket.on(message.studentFocus, m => {
36 socket.broadcast.to(aid + "_teacher").emit(message.studentFocus, m);
37 });
38 socket.on(message.studentResize, m => {
39 socket.broadcast.to(aid + "_teacher").emit(message.studentResize, m);
40 });
41 socket.on(message.studentFullscreen, m => {
42 socket.broadcast.to(aid + "_teacher").emit(message.studentFullscreen, m);
43 });
44 socket.on("disconnect", () => { //notify monitor + server
45 EvaluationModel.setDiscoTime(ObjectId(aid), number);
46 socket.broadcast.to(aid + "_teacher").emit(message.studentDisconnect, {number: number});
47 });
48 });
49 }
50 });
51 }