38a9929bcca78ec4c43b3f567384f4ab5f6d1781
1 var message
= require("./public/javascripts/utils/socketMessages.js");
2 const params
= require("./config/parameters");
4 // TODO: when teacher connect on monitor, io.of("appropriate namespace").on(connect student) { ... }
5 // --> 2 sockets on monitoring page: one with ns "/" et one dedicated to the exam, triggered after the first
6 // --> The monitoring page should not be closed during exam (otherwise monitors won't receive any more data)
8 function quizzRoom(socket
) {
11 // Student or monitor stuff
12 const isTeacher
= !!socket
.handshake
.query
.secret
&& socket
.handshake
.query
.secret
== params
.secret
;
16 // TODO: on student disconnect, too
17 socket
.on(message
.newAnswer
, m
=> { //got answer from student
18 socket
.emit(message
.newAnswer
, m
);
20 socket
.on(message
.socketFeedback
, m
=> { //send feedback to student (answers)
21 if (!!students
[m
.number
])
22 socket
.broadcast
.to(students
[m
.number
]).emit(message
.newFeedback
, { feedback:m
.feedback
});
24 socket
.on("disconnect", m
=> {
25 // Reset student array if no more active teacher connections (TODO: condition)
32 const number
= socket
.handshake
.query
.number
;
33 const password
= socket
.handshake
.query
.password
;
34 // Prevent socket connection (just ignore) if student already connected
35 if (!!students
[number
] && students
[number
].password
!= password
)
41 socket
.on(message
.newFeedback
, () => { //got feedback from teacher
42 socket
.emit(message
.newFeedback
, m
);
44 // NOTE: nothing on disconnect --> teacher disconnect trigger students cleaning
48 module
.exports = function(io
) {
50 // NOTE: if prof connected with 2 tabs and close 1, quizz should not break, thus following counter
53 io
.of("/").on("connection", socketProf
=> {
54 function closeQuizz(fullPath
) {
55 namespaces
[fullPath
].counter
--;
56 if (namespaces
[fullPath
].counter
== 0)
58 // https://stackoverflow.com/questions/26400595/socket-io-how-do-i-remove-a-namespace
59 const connectedSockets
= Object
.keys(namespaces
[fullPath
].nsp
.connected
);
60 connectedSockets
.forEach( sid
=> {
61 namespaces
[fullPath
].nsp
.connected
[sid
].disconnect();
63 namespaces
[fullPath
].nsp
.removeAllListeners();
64 delete io
.nsps
[fullPath
];
67 // Only prof account can connect default namespace
68 socketProf
.on(message
.startQuizz
, m
=> {
69 // m contient quizz ID + fullPath (initials+path+name)
70 const quizzNamespace
= io
.of(m
.fullPath
);
71 if (!namespaces
[m
.fullPath
])
73 namespaces
[m
.fullPath
] = { nsp:quizzNamespace
, counter:1 };
74 quizzNamespace
.on("connection", quizzRoom
); //après ça : prof can connect in quizz too
75 socketProf
.emit(message
.quizzReady
);
76 socketProf
.on(message
.endQuizz
, m2
=> {
77 closeQuizz(m
.fullPath
);
79 socketProf
.on("disconnect", m2
=> {
80 closeQuizz(m
.fullPath
); //TODO: this should delete all students in array
84 namespaces
[m
.fullPath
]++;