-
Notifications
You must be signed in to change notification settings - Fork 9
/
io.js
68 lines (57 loc) · 2.9 KB
/
io.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* eslint-disable global-require */
const { Server } = require("socket.io");
const {checkAccess, getInfoFromSocket, socketAuthenticate, sendInitialInfo, initializeListeners } = require("./helpers/sockets");
const {getAuthMessageAndCode, NOT_A_PARTICIPANT, NOK} = require("./helpers/apiCodes");
exports.createServer = (server, sessionMiddleware) => {
const io = new Server(server, {"perMessageDeflate": false, "allowEIO3": true, "cors": { "origin": "*", "methods": ["GET", "POST"]} });
io.use(function (socket, next) {
sessionMiddleware(socket.request, socket.request.res, next);
});
// TODO Discover what happens when server disconnects. Reconnection same socket id?
io.on("connection", async (socket) => {
try {
const user = await socketAuthenticate(socket);
const {escapeRoomId, lang, waiting, "turnId": teacherTurnId} = getInfoFromSocket(socket);
let forceLanguage = "en";
if (lang && (lang === "es" || lang === "en")) {
forceLanguage = lang;
}
let i18n = require(`./i18n/${forceLanguage}`);
if (user) {
if (user.lang && (user.lang === "es" || user.lang === "en")) {
i18n = require(`./i18n/${user.lang}`);
}
const {token, username} = user;
const {"turnId": studentTurnId, teamId, participation, erState, errorMsg, language, teamInstructions} = await checkAccess(user, escapeRoomId, i18n, waiting);
if (language && (language === "es" || language === "en")) {
i18n = require(`./i18n/${language}`);
}
socket.handshake.username = username;
socket.handshake.userId = user.id;
if (errorMsg) {
sendInitialInfo(socket, {"code": NOK, "authentication": true, token, "msg": errorMsg});
return;
}
const {code, msg} = getAuthMessageAndCode(participation, i18n);
const response = {code, "authentication": true, token, participation, msg, erState};
const turnId = studentTurnId || teacherTurnId;
if (user.isAdmin || participation && participation !== NOT_A_PARTICIPANT) {
initializeListeners(escapeRoomId, turnId, teamId, user, waiting, i18n, teamInstructions, socket);
if (turnId) {
sendInitialInfo(socket, response);
}
} else {
sendInitialInfo(socket, response);
}
} else {
sendInitialInfo(socket, {"code": NOK, "authentication": false, "msg": i18n.api.wrongCredentials});
if (socket.conn) {
socket.conn.close();
}
}
} catch (e) {
console.error(e);
}
});
return io;
};