forked from princebatra2315/Socket-Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.js
63 lines (56 loc) · 1.78 KB
/
room.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
// ROOM FUNCTION CONSTRUCTOR
function Room(id, name, creatorID) {
this.id = id; // room ID -> shortid.generate()
this.name = name; // client set in front-end -> server receives data from front-end
this.creator = creatorID; // client's unique ID/token
this.client = []; // array of clients ID
this.quantity = 0;
};
// addClient when a new client joins the room
Room.prototype.addClient = function (clientID) {
this.client.push(clientID);
this.quantity ++;
};
// removeClient when a client leaves the room
Room.prototype.removeClient = function (clientID) {
let clientIndex = -1;
for (let i = 0; i < this.client.length; i++) {
if (this.client[i].id === clientID) {
clientIndex = i;
break;
}
}
this.client.splice(clientIndex, 1);
this.quantity --;
};
// ES6 CLASS
// PUBLIC CHAT ROOM CLASS
class ROOM {
constructor (id, name, creatorID) {
this.id = id; // room ID -> generated by shortid in server
this.name = name; // client set in front-end -> server receives data from front-end
this.creator = creatorID; // client's unique ID/token
this.client = [creatorID]; // array of clients ID that are in this room
this.quantity = 1; // number of clients in this room
}
//
addClient (clientID) {
this.client.push(clientID);
this.quantity ++;
}
removeClient (clientID) {
let clientIndex = -1;
for (let i = 0; i < this.client.length; i++) {
if (this.client[i] === clientID) {
clientIndex = i;
break;
}
}
this.client.splice(clientIndex, 1);
this.quantity --;
}
changeName (newRoomName) {
this.name = newRoomName;
}
}
module.exports = ROOM;