-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
283 lines (234 loc) · 6.67 KB
/
Copy pathserver.js
File metadata and controls
283 lines (234 loc) · 6.67 KB
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
var express = require('express');
var app = express();
var port = process.env.PORT || 3000;
app.use('/', express.static('./client'));
var server = app.listen(port);
var io = require('socket.io')({
transports: ["xhr-polling"],
'polling duration': 10
}).listen(server);
var users = {};
var queue = [];
var votes = {};
var upvotes = 0;
var downvotes = 0;
var current;
var timeTotal;
var timeLeft;
var sync;
var set;
var switched;
//Helper function to reset the state of clients connected
var reset = function() {
votes = {};
upvotes = 0;
downvotes = 0;
io.emit('clearVotes');
io.emit('nextVideo', current);
io.emit('setQueue', queue);
}
io.on('connection', function(socket) {
//Receives username from client and emits username, socket id, users online back to client
socket.on('username', function(username) {
if (!userExist(users, username) || username === 'anonymous'){
io.emit('chatMessage', {username: "", message: username + " has joined"});
}
users[socket.id] = username;
io.sockets.connected[socket.id].emit('setUser', username);
io.sockets.connected[socket.id].emit('setId', socket.id);
io.emit('usersOnline', users);
});
//Sends queue information to client
socket.on('getQueue', function() {
if (queue.length) {
io.sockets.connected[socket.id].emit('setQueue', queue);
}
});
//Sends current video to client
socket.on('getCurrent', function() {
if (current) {
io.sockets.connected[socket.id].emit('setCurrent', current);
} else {
io.sockets.connected[socket.id].emit('setVolume');
}
});
//Sends vote information for current video to client
socket.on('getVotes', function() {
io.sockets.connected[socket.id].emit('changeVotes', {up: upvotes, down: downvotes});
});
//Sends video duration to client
socket.on('getTime', function() {
io.sockets.connected[socket.id].emit('setTime', timeTotal - timeLeft || timeTotal);
});
//Emits message to all clients
socket.on('sendMessage', function(data) {
io.emit('chatMessage', data);
});
//Emits alert message only to sender
socket.on('sendAlert', function(data) {
socket.emit('chatMessage', data);
});
//Emits data to only specific client (private message)
socket.on('privateMessage', function(data){
io.sockets.sockets[data.sendToSocketId].emit('chatMessage', data);
socket.emit('chatMessage', data);
});
//Send error message to user on submit
socket.on('errorMessage', function(data){
socket.emit('chatMessage', data);
});
//Check if user exists
socket.on('checkUser', function(username){
var exist = false;
for (var key in users){
if (users[key] === username){
exist = true;
}
}
socket.emit('userExist', exist);
});
function userExist(object, username){
for (var key in object){
if (users[key] === username){
return true;
}
}
return false;
}
socket.on('enqueue', function(data) {
if (current) {
queue.push(data);
io.emit('addVideo', data);
} else {
set = false;
current = data;
reset();
}
});
socket.on('dequeue', function(data) {
var id = socket.id;
if (id.slice(2) === data.socket) {
io.emit('removeVideo', data.id);
}
});
socket.on('updateQueue', function(data) {
queue = data;
socket.broadcast.emit('refreshQueue', queue);
});
socket.on('easterEgg', function() {
if (queue.length) {
queue.unshift({ id: 'SbyZDq76T74',
title: 'Meow Mix song',
username: 'Meow Mode',
socket: current.socket });
current = queue.shift();
reset();
} else {
current = { id: 'SbyZDq76T74',
title: 'Meow Mix song',
username: 'Meow Mode',
socket: socket.id.slice(2) };
reset();
}
});
//Receives video ended from client and sets current to next in queue
//Jerry-rigged so that the fastest client emits the switch and locks other clients out for 5 seconds
socket.on('ended', function() {
if (!switched) {
switched = true;
set = false;
current = queue.shift();
reset();
setTimeout(function() {
switched = false;
}, 5000);
}
});
//Allows skipping if event is emitted by client who enqueued video
socket.on('skip', function(easterEgg) {
var id = socket.id;
if (current && id.slice(2) === current.socket || easterEgg) {
if (queue.length) {
set = false;
current = queue.shift();
reset();
} else {
set= false;
current = null;
reset();
io.emit('stopVideo');
}
}
});
//Start the video sync clock
socket.on('setDuration', function(duration) {
if (!set) {
set = true;
timeTotal = duration;
timeLeft = duration;
clearInterval(sync);
sync = setInterval(function() {
timeLeft--;
if (timeLeft === 0) {
clearInterval(sync);
}
}, 1000);
}
});
//Removes client vote information and delete client on client disconnect
socket.on('disconnect', function() {
if (votes[socket.id] === 'up') {
upvotes--;
}
if (votes[socket.id] === 'down') {
downvotes--;
}
io.emit('changeVotes', {up: upvotes, down: downvotes});
io.emit('chatMessage', {username: "", message: users[socket.id] + " has left"});
delete users[socket.id];
io.emit('usersOnline', users);
});
//Receives upvote from client and updates vote information; emitting to all clients
socket.on('upVote', function() {
if (votes[socket.id] === 'down') {
votes[socket.id] = 'up';
downvotes--;
upvotes++;
}
if (votes[socket.id] === undefined) {
votes[socket.id] = 'up';
upvotes++;
}
io.emit('changeVotes', {up: upvotes, down: downvotes});
});
socket.on('downVote', function(){
if(votes[socket.id] === 'up'){
votes[socket.id] = 'down';
upvotes--;
downvotes++;
}
if(votes[socket.id] === undefined) {
votes[socket.id] = 'down';
downvotes++;
}
//Skips current video if more haters than non-haters
var haters = downvotes/Object.keys(users).length;
if(haters > 0.5) {
if (queue.length) {
set = false;
current = queue.shift();
reset();
} else {
set= false;
current = null;
reset();
io.emit('stopVideo');
}
}
io.emit('changeVotes', {up: upvotes, down: downvotes});
});
//Sends clock time to client when requested
socket.on('getSync', function() {
io.sockets.connected[socket.id].emit('setSync', timeTotal - timeLeft || timeTotal);
});
});