-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketsCustom.js
83 lines (70 loc) · 2.29 KB
/
socketsCustom.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
/**
* Created by Pascal on 06.03.2016.
*/
module.exports = function (io) {
return new SocketsCustom(io);
};
function SocketsCustom(io) {
this.__io = io;
/**
*
* @type {{roomName (String), roomPwd (String), adminPwd (String), lecturerPeer (Socket)}}
* @private
*/
this.__roomsInfo = {};
}
SocketsCustom.prototype = {
constructor: SocketsCustom,
roomExistsAsSocket: function (roomName) {
var nsp = this.__io.nsps['/'];
var adapter = nsp.adapter;
var rooms = adapter.rooms;
var room = rooms[roomName];
return !!room;
},
roomExists: function (roomName) {
return this.__roomsInfo.hasOwnProperty(roomName);
},
isRoomFull: function (config, name, cb) {
return (config.rooms && config.rooms.maxClients > 0 &&
clientsInRoom(name) >= config.rooms.maxClients);
},
createRoom: function (roomName, adminPwd, roomPwd, lecturerPeer) {
this.__roomsInfo[roomName] = {
adminPwd: adminPwd,
roomPwd: roomPwd,
lecturerPeer: lecturerPeer
};
},
getLecturerPeerOfRoom: function (roomName) {
return this.__roomsInfo[roomName].lecturerPeer;
},
removeRoomOnLecturerDisconnect: function (clientId, roomName) {
if (this.isLecturer(clientId, roomName)) {
delete this.__roomsInfo[roomName];
}
},
getRooms: function () {
var that = this;
var rooms = [];
Object.keys(this.__roomsInfo).forEach(function (roomName) {
rooms.push({
roomName: roomName,
hasRoomPwd: !!that.__roomsInfo[roomName].roomPwd
});
});
return rooms;
},
isRoomPwdCorrect: function (roomName, pwd) {
return this.__roomsInfo[roomName].roomPwd === pwd;
},
isAdminPwdCorrect: function (roomName, adminPwd) {
return this.__roomsInfo[roomName].adminPwd === adminPwd;
},
isLecturer: function (clientId, roomName) {
if (!roomName || !this.__roomsInfo.hasOwnProperty(roomName)) { // peer connects to socket server on browser load. Room might not exist until he manually creates/joins it
return false;
}
return clientId === this.__roomsInfo[roomName].lecturerPeer.id;
}
}; // prototype