thoughts about time, refactor statements component
[qomet.git] / sockets.js
CommitLineData
e5ec7dea 1const message = require("./public/javascripts/utils/socketMessages");
e99c53fb 2const params = require("./config/parameters");
71d1ca9c 3const AssessmentModel = require("./models/assessment");
e5ec7dea 4const ObjectId = require("bson-objectid");
e99c53fb 5
f6648c37
BA
6module.exports = function(io)
7{
29c8b391 8 io.of("/").on("connection", socket => { //student or monitor connexion
f6648c37 9 const aid = socket.handshake.query.aid;
f6648c37 10 const isTeacher = !!socket.handshake.query.secret && socket.handshake.query.secret == params.secret;
71d1ca9c 11
f6648c37
BA
12 if (isTeacher)
13 {
29c8b391 14 socket.join(aid + "_teacher");
f6648c37 15 socket.on(message.allAnswers, m => { //send feedback to student (answers)
29c8b391 16 socket.broadcast.to(aid + "_student").emit(message.allAnswers, m);
e5ec7dea 17 });
e99c53fb 18 }
f6648c37
BA
19 else //student
20 {
21 const number = socket.handshake.query.number;
22 const password = socket.handshake.query.password;
71d1ca9c 23 AssessmentModel.checkPassword(ObjectId(aid), number, password, (err,ret) => {
f6648c37
BA
24 if (!!err || !ret)
25 return; //wrong password, or some unexpected error...
29c8b391
BA
26 AssessmentModel.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 });
2bada710
BA
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 });
29c8b391 44 socket.on("disconnect", () => { //notify monitor + server
43828378 45 AssessmentModel.setDiscoTime(ObjectId(aid), number);
29c8b391 46 socket.broadcast.to(aid + "_teacher").emit(message.studentDisconnect, {number: number});
e99c53fb 47 });
f6648c37
BA
48 });
49 }
e99c53fb
BA
50 });
51}