Commit | Line | Data |
---|---|---|
e5ec7dea | 1 | const message = require("./public/javascripts/utils/socketMessages"); |
e99c53fb | 2 | const params = require("./config/parameters"); |
29c8b391 | 3 | const AssessmentEntity = require("./entities/assessment"); |
71d1ca9c | 4 | const AssessmentModel = require("./models/assessment"); |
e5ec7dea | 5 | const ObjectId = require("bson-objectid"); |
e99c53fb | 6 | |
f6648c37 BA |
7 | module.exports = function(io) |
8 | { | |
29c8b391 | 9 | io.of("/").on("connection", socket => { //student or monitor connexion |
f6648c37 | 10 | const aid = socket.handshake.query.aid; |
f6648c37 | 11 | const isTeacher = !!socket.handshake.query.secret && socket.handshake.query.secret == params.secret; |
71d1ca9c | 12 | |
f6648c37 BA |
13 | if (isTeacher) |
14 | { | |
29c8b391 | 15 | socket.join(aid + "_teacher"); |
f6648c37 | 16 | socket.on(message.allAnswers, m => { //send feedback to student (answers) |
29c8b391 | 17 | socket.broadcast.to(aid + "_student").emit(message.allAnswers, m); |
e5ec7dea | 18 | }); |
e99c53fb | 19 | } |
f6648c37 BA |
20 | else //student |
21 | { | |
22 | const number = socket.handshake.query.number; | |
23 | const password = socket.handshake.query.password; | |
71d1ca9c | 24 | AssessmentModel.checkPassword(ObjectId(aid), number, password, (err,ret) => { |
f6648c37 BA |
25 | if (!!err || !ret) |
26 | return; //wrong password, or some unexpected error... | |
29c8b391 BA |
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}); | |
e99c53fb | 36 | }); |
f6648c37 BA |
37 | }); |
38 | } | |
e99c53fb BA |
39 | }); |
40 | } |