-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
167 lines (165 loc) · 4.86 KB
/
socket.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
module.exports = (io, db) => {
const {join} = require('path');
const fs = require('fs');
var library = db.library.find();
var download = require('./videoDL.js');
var queue = [];
var userArr = [];
var host = "";
var playing = false;
var stopped = false;
var downloading = false;
io.on("connection", (socket) => {
if (userArr.length < 4) {
if (host === "") {
console.log("Host connected: ", socket.id);
host = socket;
socket.emit("connection", "host");
}
else {
console.log("User connected: ", socket.id);
userArr.push(socket);
socket.emit("connection", "user");
}
socket.emit("library", library);
socket.emit("queue", queue);
}
else {
console.log("Force Disconnect: Too many users");
socket.disconnect();
}
socket.on('disconnect', () => {
if (host === socket) {
console.log("Host disconnected", socket.id);
for (var j=0; j < userArr.length; j++) {
userArr[j].emit("close");
}
host = "";
queue = [];
playing = false;
stopped = false;
}
else {
var i = userArr.indexOf(socket);
console.log("User disconnected", socket.id);
userArr.splice(i, 1);
}
});
socket.on('playSong', () => {
console.log("Play Song")
if (!playing && stopped && queue.length) {
io.emit('play', queue[0])
queue.splice(0, 1);
io.emit('queue', queue);
playing = true;
stopped = false;
}
});
socket.on('skipSong', () => {
console.log("Skip Song")
if (!stopped && playing) {
if (queue.length) {
io.emit('play', queue[0])
queue.splice(0, 1);
io.emit('queue', queue);
}
else {
io.emit('play', false);
playing = false;
}
}
});
socket.on('stopSong', () => {
console.log("Stop Song")
console.log(playing)
console.log(stopped)
if (playing && !stopped) {
playing = false;
stopped = true;
io.emit('play', false);
io.emit('stop');
}
});
socket.on('addQueue', (song) => {
console.log('Add Queue: '+song['code'])
queue.push(song);
if (queue.length && !playing && !stopped) {
io.emit('play', queue[0]);
playing = true;
queue.splice(0, 1);
}
io.emit('queue', queue);
});
socket.on('removeQueue', (index) => {
console.log('Remove Queue: '+queue[index]['code'])
queue.splice(index, 1);
io.emit('queue', queue);
});
socket.on('addLibrary', (song) => {
if (!downloading) {
downloading = true;
io.emit('addProgress', ['start', 'Currently Downloading: '+song['title']]);
download(song, library, (song) => {
console.log('Adding to Library: '+ song['title']);
db.library.save({
code: song['code'],
title: song['title'],
artist: song['artist'],
// tags: song['tags'],
url: 'songs/'+song['code']+'.mp4'
});
console.log('Adding Finish: '+ song['title']);
library = db.library.find();
downloading = false;
socket.emit('addSuccess', song['title']+" successfully added to library");
io.emit('addProgress', ['end', 'Download Finished']);
io.emit('library', library);
}, (error) => {
downloading = false;
console.log(error);
socket.emit('addFail', "Failed to add "+song['title']+" to library");
io.emit('addProgress', ['end', 'Download Failed']);
});
}
else {
socket.emit('addFail', 'Another song is currently being downloaded. Try again later');
}
})
socket.on('editLibrary', (song) => {
db.library.update({code: song.code}, {title: song.title, artist: song.artist});
library = db.library.find();
io.emit('library', library);
})
socket.on('deleteSong', (song) => {
fs.unlink(join(__dirname,'songs/'+song.code+'.mp4'), (err) => {
if (err) {
console.log(err);
io.emit('addProgress', ['end', 'Download Failed']);
// failed = false;
return;
}
// if no error, file has been deleted successfully
db.library.remove({code: song.code});
library = db.library.find();
console.log('Song deleted!');
io.emit('library', library);
});
})
socket.on('songEnd', () => {
console.log('Song End')
if (queue.length) {
io.emit('play', queue[0])
queue.splice(0, 1);
io.emit('queue', queue);
}
else {
playing = false;
io.emit('play', false);
}
})
socket.on('speed', (direction, pbr) => {
console.log('Speed ' + direction);
io.emit('speed', direction, pbr)
})
})
}