-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
211 lines (191 loc) · 6.02 KB
/
index.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
/* eslint-env node */
//Server
const SOCKETPORT = 3000,
path = require("path"),
siofu = require("socketio-file-upload"),
fs = require("fs"),
schedule = require("node-schedule");
var server,
roomManager,
dbClient;
const AppServer = require("./server/AppServer.js"),
DBManager = require("./server/MongoDB/DBManager.js"),
express = require("express"),
RoomManager = require("./server/RoomManager.js"),
httpServer = require("http").createServer(express),
appDir = path.join(__dirname, "../", process.argv[2]),
options = { cors: true, origin: appDir },
io = require("socket.io")(httpServer, options),
uri =
"mongodb+srv://Admin:[email protected]/WatchMatesDB?retryWrites=true&w=majority",
DELETE_INTERVAL_FOR_SCHEDULE = "0 3 * * *",
DELETE_AFTER_IN_MS = 604800000,
convertDate = (date, time) => {
let exportDate = date + "T" + time + ":00";
return new Date(exportDate);
};
io.on("error", e => {
// eslint-disable-next-line no-console
console.error(e);
});
// handles all socket messages sent to server
io.on("connection", (socket) => {
let uploader = new siofu(),
roomID;
uploader.on("error", e => {
// eslint-disable-next-line no-console
console.error(e);
});
//Client Enters Room Sockets
//createRoom
socket.on("createRoom", () => {
let url = roomManager.createUrl(),
currentDate = new Date(Date.now());
dbClient.addRoom(url, currentDate);
server.addRoom(url);
socket.emit("changeUrl", url);
});
//enterRoom
socket.on("clientEntersRoom", url => {
roomID = url.split("/").pop();
uploader.dir = path.join(__dirname, "/data/" + roomID);
createRoomFolder(uploader.dir);
uploader.listen(socket);
dbClient.getPlaylist(roomID).then(e => {
socket.emit("loadPlaylist", e[0].playlist);
socket.broadcast.emit("sendDataRequestToClients", url);
});
});
// init
socket.on("currentTrackInfoToServer", (url, currentTrack) => {
socket.broadcast.emit("currentTrackInfoToClients", url,
currentTrack);
});
//Synchronous Video Sockets
//onVideoPlayed
socket.on("videoPlayedToServer", (url, time) => {
io.emit("videoPlayedToClients", url, time);
});
//onVideoPaused
socket.on("videoPausedToServer", (url) => {
io.emit("videoPausedToClients", url);
});
//onVideoEnded
socket.on("videoEndedToServer", (url, currentTrack) => {
io.emit("videoEndedToClients", url, currentTrack);
});
//Synchronous Playlist Sockets
//drag&drop
socket.on("DragDropPositionToServer", (roomID, iDrag, iDrop) => {
io.emit("DragDropPositionToClients", roomID, iDrag, iDrop);
dbClient.changePlaylistPosition(roomID.split("/").pop(), iDrag,
iDrop);
});
//videoclick
socket.on("videoClickToServer", (url, currentTrack) => {
io.emit("videoClickToClients", url, currentTrack);
});
//videokey
socket.on("videoKeyToServer", (url, currentTrack) => {
io.emit("videoKeyToClients", url, currentTrack);
});
//upload File
socket.on("fileUpload", (roomID, srcName, name) => {
let tempSrc = roomID + "/" + name + "." + srcName.split(".").pop(),
playlistObject = {
roomID: roomID,
playlistObject: {
src: tempSrc,
title: tempSrc.split("/").pop(),
},
};
io.emit("playlistObjectToClients", playlistObject);
dbClient.addPlaylistEntry(roomID, tempSrc);
});
//delete File
socket.on("deleteNumberToServer", (roomID, numberDelete) => {
io.emit("deleteNumberToClients", roomID, numberDelete);
dbClient.getPlaylist(roomID.split("/").pop()).then(e => deleteFile(
"data/" + e[0].playlist[numberDelete]));
dbClient.deletePlaylistEntry(roomID.split("/").pop(), numberDelete);
});
//LiveChat Sockets
//receive Message on Server
socket.on("MessageToServer", (message, nickname, room) => {
socket.broadcast.emit("MessageToClients", message, nickname, room);
});
//Basic Sockets
//delete
socket.on("dateToServer", (date) => {
let url = roomManager.createUrl();
dbClient.addRoom(url, convertDate(date.date, date.time));
server.addRoom(url);
socket.emit("urlToClient", url);
});
//url entered
socket.on("URLEnteredInTextField", (roomID) => {
dbClient.getRoom(roomID).then((room) => {
socket.emit("URLFound", room[0]);
});
});
//deleteFile
socket.on("deleteFile", (roomID, srcName, name) => {
let tempSrc = "data/" + roomID + "/" + name + "." + srcName.split(
".").pop();
deleteFile(tempSrc);
});
});
httpServer.listen(SOCKETPORT, function () {
//console.log("Ich höre auf socket IO Port 3000");
});
function createRoomFolder(roomDir) {
fs.mkdirSync(roomDir, { recursive: true });
}
function deleteFile(src) {
fs.unlink(src, err => {
if (err) {
// eslint-disable-next-line no-console
console.log(err);
return;
}
});
}
/**
* Starts webserver to serve files from "/app" folder
*/
function init() {
// Access command line parameters from start command (see package.json)
let appDirectory = process.argv[2],
appPort = process.argv[3];
// port to use for serving static files
server = new AppServer(appDirectory);
server.start(appPort);
roomManager = new RoomManager();
dbClient = new DBManager(uri);
checkForOldRooms();
dbClient.getOpenRooms().then((e) => server.openRooms(e));
}
/*checks every day at 3am if there are any unused rooms to delete
and delete the files from the server and from the database*/
function checkForOldRooms() {
schedule.scheduleJob(DELETE_INTERVAL_FOR_SCHEDULE, () => {
dbClient.getOpenRooms().then((rooms) => {
rooms.forEach(room => {
if (room.date === undefined) {
return;
}
let roomDateInMS = room.date.getTime();
if (Date.now() - roomDateInMS > DELETE_AFTER_IN_MS) {
dbClient.deleteRoom(room.url);
fs.readdir(("data/" + room.url), (err, files) => {
if (err) { return; }
files.forEach(file => {
deleteFile("data/" + room.url + "/" + file);
});
});
}
});
});
});
}
init();