-
Notifications
You must be signed in to change notification settings - Fork 1
/
usersession-server.js
90 lines (79 loc) · 2.1 KB
/
usersession-server.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Users sessions, consisting of SID, SocketID pairs
// Array of dicts: {sid: string, socketid: string, sub : string}
var usersessions = []
/**
* Checks if a user session is existing with the given SID.
*
* @param {*} sid SID of the user session
* @return {*} true if existing, false if not
*/
function isUserSessionExisting(sid) {
for (const s of usersessions) {
if (s.sid === sid) {
return true;
}
}
return false;
}
/**
* Creates a user session with the given SID, if not existing already.
*
* @param {*} sid SID of the user session
*/
function createUserSession(sid, sub) {
if (!isUserSessionExisting(sid)) {
// Creating new user session, with empty socketid
const newusersession = {sid: sid, sub: sub, socketid: null}
usersessions.push(newusersession);
}
}
/**
* Adds the SocketID to the SID SocketID user session pair.
*
* @param {*} sid sid of the user session
* @param {*} socketid SocketID of the user session
*/
function updateUserSessionSocketID(sid, socketid) {
for (const s of usersessions) {
if (s.sid === sid) {
s.socketid = socketid;
break;
}
}
}
/**
* Removes a user session with the given SID.
*
* @param {*} sid SID of the user session
*/
function removeUserSession(sid) {
usersessions = usersessions.filter(s => s.sid !== sid);
}
/**
* Finds the matching SID for a given SocketID for the user session.
*
* @param {*} socketid SocketID of the user session
* @return {*} SID if match found, else null
*/
function getUserSessionSID(socketid) {
for (const s of usersessions) {
if (s.socketid == socketid) {
return s.sid;
}
}
return null;
}
/**
* Returns the sub id for a given session id.
*
* @param {*} sid the sid of the user session
*/
function getUserSubID(sid) {
for (const s of usersessions) {
if (s.sid == sid) {
return s.sub;
}
}
return null;
}
module.exports = { createUserSession, updateUserSessionSocketID, removeUserSession, getUserSessionSID, isUserSessionExisting, getUserSubID };