-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
293 lines (250 loc) · 8.34 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
const express = require("express");
const socketIO = require("socket.io");
const path = require("path");
console.log("going to reset the roomsInfo");
let roomsInfo = {};
let savedMessages = {};
const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, "index.html");
// var cb1 = function(req, res, next) {
// res.send("hello", INDEX);
// next();
// };
const server = express()
// .use((req, res) => res.sendFile(INDEX))
.use(express.static(path.join(__dirname, "public")))
.get("/", function(req, res) {
res.sendFile(INDEX);
})
.get("/contact", function(req, res) {
res.sendFile(INDEX);
})
.get("/Roomname=:id", function(req, res) {
res.sendFile(INDEX);
})
.get("*", function(req, res) {
res.redirect("/");
})
.listen(PORT, () => console.log(`Listening on ${PORT}`));
const io = socketIO(server);
io.on("connection", function(socket) {
/* User Connection/Disconnection Starts */
console.log(
" "
);
console.log("a user connected");
socket.on("disconnect", function() {
console.log("a user disconnected");
console.log("socket.userRoom: ", socket.userRoom);
deletePeopleInRoomInRoomsInfo(socket.userRoom);
deleteMessagesInSavedMessagesObjIfRoomIsEmpty(socket.userRoom);
io.sockets.in(socket.userRoom).emit("user got disconnected", {
name: socket.username,
});
sendNewRoomData();
});
socket.on("user joined", function(data) {
socket.username = data.name;
io.sockets.in(socket.userRoom).emit("got new user", {
name: socket.username,
});
});
/* User Ends */
/* Room Starts */
// console.log("length of people on website: ", io.sockets.adapter.rooms);
/* Manage Rooms Function Starts */
function isThisRoomInRoomsInfo(roomName) {
if (roomsInfo.hasOwnProperty(roomName) === false) {
// console.log(roomsInfo[key])
// console.log(false);
return false;
}
}
function addRoomInRoomsInfo(roomName) {
if (roomsInfo[roomName] === undefined) {
console.log("Adding room to RoomsInfo: ", roomName);
roomsInfo[roomName] = {};
} else {
console.log("Didn't add Room b/c it was already there: ", roomName);
}
}
function addPeopleInRoomInRoomsInfo(roomName) {
console.log(
"Adding People in: ",
roomName,
", People Contains Before Adding: ",
roomsInfo[roomName].people
);
if (isNaN(roomsInfo[roomName].people)) {
console.log(
"The Room.people was not already defined, that is why it came here"
);
roomsInfo[roomName].people = +1;
} else {
roomsInfo[roomName].people += 1;
}
}
function deletePeopleInRoomInRoomsInfo(roomName) {
if (roomsInfo[roomName] !== undefined) {
console.log(
"Deleting People From ",
roomName,
", People Contains Before Deleting: ",
roomsInfo[roomName].people
);
roomsInfo[roomName].people -= 1;
}
}
function addUrlToRoomInRoomsInfo(roomName, urlLink) {
roomsInfo[roomName].url = urlLink;
}
function deleteRoomInRoomsInfoIfEmpty(roomName) {
if (roomsInfo[roomName] !== undefined) {
if (roomsInfo[roomName].people == 0) {
console.log(
"Deleting Room: ",
roomName,
" People Contains: ",
roomsInfo[roomName].people
);
delete roomsInfo[roomName];
}
}
}
/* Manage Rooms Function Ends */
function sendNewRoomData() {
io.sockets.emit("rooms count", {
namesOfRooms: io.sockets.adapter.rooms,
peopleCountInRoom: roomsInfo,
});
}
function keepSavedMessages(room, userName, msg) {
if (!Array.isArray(savedMessages[room])) {
savedMessages[room] = [];
}
savedMessages[room].push({ name: [userName], message: msg });
if (savedMessages[room].length > 10) {
savedMessages[room].shift();
}
}
function deleteMessagesInSavedMessagesObjIfRoomIsEmpty(roomName) {
if (roomsInfo[roomName] == undefined && roomName !== "global") {
delete savedMessages[roomName];
}
}
function sendSavedMessagesToClient(socketId, roomName) {
if (savedMessages.hasOwnProperty(roomName)) {
io.sockets.to(socketId).emit("send saved messages to single client", {
data: savedMessages[roomName],
});
}
}
socket.on("room", function(room) {
console.log(`this is room=>>`, room);
// console.log("this is the socketid of this user: socket.id", socket.id);
socket.userRoom = room; // Setting up new room for the session of user
if (isThisRoomInRoomsInfo(room) === false) {
addRoomInRoomsInfo(room);
}
addPeopleInRoomInRoomsInfo(room);
console.log("LINE 123 RoomsInfo Obj: ", roomsInfo);
if (Object.keys(io.sockets.adapter.sids[socket.id]).length >= 2) {
let OldRoom = socket.rooms[Object.keys(socket.rooms)[1]];
deletePeopleInRoomInRoomsInfo(OldRoom);
deleteRoomInRoomsInfoIfEmpty(OldRoom);
deleteMessagesInSavedMessagesObjIfRoomIsEmpty(OldRoom);
socket.leave(OldRoom); // Leaving the old room
console.log("OLD ROOM: ", OldRoom);
}
socket.join(room); // Joining the new room
// console.log("Listing All Rooms: " + io.sockets.adapter.rooms);
sendSavedMessagesToClient(socket.id, room);
sendNewRoomData();
// console.log("socket.userRoom: ", socket.userRoom);
// console.log("roomsInfo[socket.userRoom]: ", roomsInfo[socket.userRoom]);
if (room !== "global") {
console.log("roomsInfo Obj: ", roomsInfo);
if (
roomsInfo[socket.userRoom].url !== "" &&
roomsInfo[socket.userRoom].url !== undefined
) {
console.log(
"roomsInfo[socket.userRoom].url: ",
roomsInfo[socket.userRoom].url
);
console.log("it should send url to the specifi person now");
let videoUrl = roomsInfo[socket.userRoom].url;
socket.emit("sending url to everyone or to client", {
url: videoUrl,
toSingleClientOnly: true,
});
}
} else {
socket.emit("sending url to everyone or to client", {
url: "",
toSingleClientOnly: true,
});
}
});
/* Room Ends */
/* User Joins Through Url Starts */
socket.on("give me room video state and time", function() {
socket
.to(socket.userRoom)
.emit(
"give your room video state and time to everyone in room except sender"
);
});
/* User Joins Through Url Ends */
/* Chat Section Starts in Server */
socket.on("chat message", function(data) {
console.log(
"Room: " +
socket.userRoom +
", Msg: " +
data.msg +
", User: " +
socket.username
);
keepSavedMessages([socket.userRoom], [socket.username], data.msg);
// console.log("savedMessages: ", savedMessages);
// savedMessages[socket.userRoom].push("sdfdsfds");
console.log(data.msg);
io.sockets.in(socket.userRoom).emit("new message", {
msg: data.msg,
name: socket.username,
});
});
/* Chat Ends in Server */
/* Youtube Section Starts in Server */
socket.on("sending url to server", function(data) {
console.log(`server: sending url to server`, data);
// roomsInfo[socket.userRoom] = data.url;
if (!isThisRoomInRoomsInfo(socket.userRoom)) {
addRoomInRoomsInfo(socket.userRoom);
}
addUrlToRoomInRoomsInfo(socket.userRoom, data.url);
console.log("this is rooms Info object: ", roomsInfo);
// console.log('second one ', data.url);
io.sockets
.in(socket.userRoom)
.emit("sending url to everyone or to client", {
url: data.url,
});
// console.log(socket.userRoom);
// console.log(roomsInfo);
});
socket.on("play video now to server", function(room) {
io.sockets.in(room).emit("playing video for everyone");
});
socket.on("pause video now to server", function(room) {
io.sockets.in(room).emit("pausing video for everyone");
});
socket.on("new time send to server", function(data) {
console.log(`the following: room=>${socket.userRoom} time=>${data.time}`);
io.sockets.in(socket.userRoom).emit("send this time to everyone", {
time: data.time,
});
});
/* Youtube Ends in Server */
});