-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
208 lines (170 loc) · 6.37 KB
/
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const rooms = {};
const clients = {};
app.use(express.static(__dirname + '/Front-End/dist'));
app.get('/', function(req, res) {
res.sendFile(__dirname + '/Front-End/dist/index.html');
});
server.listen(process.env.PORT || 5000);
function clientInit(socket, clientObj){
var clientId = socket.id;
if(clients.hasOwnProperty(clientId)){
console.log(clientId + ": Client Already Exist!");
//return new Error(clientId + ": Client Already Exist!");
return null;
}else{
clientObj.permLevel = 2;
clientObj.roomId = null;
clientObj.clientId = clientId;
return clientObj;
}
}
function roomInit(roomObj){
var roomId = roomObj.roomId;
if(rooms.hasOwnProperty(roomId)){
console.log(roomId + ": Room Already Exist!");
//return new Error(roomId + ": Room Already Exist!");
return null;
}else{
roomObj.host = null;
roomObj.users = {};
roomObj.playlist = {};
return roomObj;
}
}
function createRoom(socket, hostObj, roomObj){
hostObj = clientInit(socket, hostObj);
roomObj = roomInit(roomObj);
if(roomObj !== null){
socket.join(roomObj.roomId);
hostObj.permLevel = 0;
hostObj.roomId = roomObj.roomId;
clients[hostObj.clientId] = hostObj;
roomObj.host = hostObj.clientId;
roomObj.users[hostObj.clientId] = hostObj;
rooms[roomObj.roomId] = roomObj;
roomUsersUpdate(roomObj.roomId);
console.log(hostObj.clientId + " created a room "+roomObj.roomId);
}else{
io.sockets.connected[socket.id].disconnect();
}
}
function joinRoom(socket, roomId, clientObj){
clientObj = clientInit(socket, clientObj);
if(clientObj !== null && rooms.hasOwnProperty(roomId)){
clientObj.roomId = roomId;
socket.join(roomId);
clients[socket.id] = clientObj;
rooms[roomId].users[socket.id] = clientObj;
roomUsersUpdate(roomId);
if(rooms[roomId].playlist.hasOwnProperty('playing')){
io.to(socket.id).emit('playlist update', rooms[roomId].playlist);
}
console.log(clientObj.clientId + " joined a room "+roomId);
}else{
io.sockets.connected[socket.id].disconnect();
}
}
function giveAdminPerm(socket, roomId, userId){
if(clients[socket.id].permLevel === 0 && rooms[roomId].users.hasOwnProperty(userId)){
if(clients[userId].permLevel === 2){
clients[userId].permLevel = 1;
rooms[roomId].users[userId].permLevel = 1;
roomUsersUpdate(roomId);
}else if(clients[userId].permLevel === 1){
clients[userId].permLevel = 2;
rooms[roomId].users[userId].permLevel = 2;
roomUsersUpdate(roomId);
}else{
//new Error(userId + "is now admin");
console.log(userId + "is now admin");
}
}else{
//new Error("Unauthorized Operation or User Doesn't Exist");
console.log("Unauthorized Operation or User Doesn't Exist");
io.sockets.connected[socket.id].disconnect();
}
}
function deleteUser(roomId, clientId){
delete clients[clientId];
delete rooms[roomId].users[clientId];
}
function clientLeft(socket){
//clients[socket.id] = clients[socket.id] || {};
//delete clients[socket.id]
if(clients.hasOwnProperty(socket.id) && (rooms.hasOwnProperty(clients[socket.id].roomId) &&
rooms[clients[socket.id].roomId].users.hasOwnProperty(socket.id))){
var clientsRoomId = clients[socket.id].roomId;
if(clients[socket.id].permLevel === 0){
deleteUser(clientsRoomId, socket.id);
for(var client in rooms[clientsRoomId].users){
//io.sockets.sockets[client].leave(clientsRoomId);
io.sockets.connected[client].disconnect();
delete clients[client];
}
delete rooms[clientsRoomId];
}else{
deleteUser(clientsRoomId, socket.id);
roomUsersUpdate(clientsRoomId);
}
}else{
//new Error("Client or Room Doesn't Exist!");
console.log("Client or Room Doesn't Exist!");
}
}
function roomUsersUpdate(roomId){
io.sockets.to(roomId).emit('users update', rooms[roomId].users);
}
function roomPlaylistUpdate(socket, roomId, playlistObjList){
if(rooms.hasOwnProperty(roomId) && rooms[roomId].users.hasOwnProperty(socket.id)
&& rooms[roomId].users[socket.id].permLevel <= 1){
rooms[roomId].playlist = playlistObjList;
socket.to(roomId).emit('playlist update', playlistObjList);
}else{
//new Error("Client Not Authorized or Room Doesn't Exist");
console.log("Client Not Authorized or Room Doesn't Exist");
}
}
function changeNickName(socket, roomId, new_nickName){
if(rooms.hasOwnProperty(roomId) && rooms[roomId].users.hasOwnProperty(socket.id)){
rooms[roomId].users[socket.id].nickName = new_nickName;
clients[socket.id].nickName = new_nickName;
roomUsersUpdate(roomId);
}else{
//new Error("Room or Client Doesn't Exist");
console.log("Room or Client Doesn't Exist");
}
}
io.on('connection', function (socket){
console.log(socket.id + ' connected.');
socket.on('disconnect', function(reason){
console.log(socket.id + ' disconnected. ' + reason);
clientLeft(socket);
});
socket.on('kick user', function(clientId){
io.sockets.connected[clientId].disconnect();
});
socket.on('change nick', function(roomId, new_nickName){
changeNickName(socket, roomId, new_nickName);
});
socket.on('give admin', function(roomId, userId){
giveAdminPerm(socket, roomId, userId);
});
/* */
socket.on('create room', function(hostObj, roomObj){
createRoom(socket, hostObj, roomObj);
});
socket.on('join room', function(roomId, clientObj){
joinRoom(socket, roomId, clientObj);
});
/* */
socket.on('playlist', function(roomId, playlistObjList){
roomPlaylistUpdate(socket, roomId, playlistObjList);
});
socket.on('player', function(roomId, eventObj){
socket.to(roomId).emit('player', eventObj);
});
});