-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
110 lines (93 loc) · 3.27 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
"use strict";
var http = require('http');
var staticServer = require('node-static');
var socketio = require('socket.io');
var playlist = require('./Playlist.js');
var medialist = require('./Medialist.js');
var formidable = require('formidable');
var fileServer = new staticServer.Server(__dirname + '/public');
var PORT = 8085;
var server = http.createServer(function(request, response){
if (request.url === '/upload'){
var form = new formidable.IncomingForm();
try {
form.parse(request, function(err, fields, files){
medialist.saveMedia(files, function(){
websock.sockets.in('users').emit('medialist updated', medialist.list);
});
response.end('ok');
});
} catch(ex) {
console.log(ex);
}
}else{
request.addListener('end', function(){
fileServer.serve(request, response);
});
}
});
server.listen(PORT);
var websock = socketio.listen(server);
websock.configure(function(){
websock.disable('log');
});
websock.sockets.on('connection', function(socket){
socket.join('users');
socket.emit('init', {
playlist: playlist.list,
medialist: medialist.list,
});
socket.on('song selected', function(data){
var song = data.song;
var username = data.username;
var ip = socket.handshake.address.address;
var ret = playlist.addSong(song, 'local', ip, username);
if (ret === 0){
if (playlist.list.length === 1){
playlist.play();
}
websock.sockets.in('users').emit('playlist updated', playlist.list);
}else{
switch (ret.type) {
case 'secondsTillNextSong':
socket.emit('message', 'Whao, Dont SPAM! Please wait '
+ ret.time + ' seconds before adding next song');
break;
case 'maxSongLimitPerIP':
socket.emit('message', 'You already got ' + ret.count
+ ' songs queued, give others a chance!');
break;
}
}
});
socket.on('song removed', function(songIndex) {
var ip = socket.handshake.address.address;
if (ip === playlist.list[songIndex].addedByIP) {
playlist.removeSong(songIndex);
websock.sockets.in('users').emit('playlist updated', playlist.list);
} else {
socket.emit('message', 'Sorry, you can only remove your own songs. \
Yeh I know the ideal thing would be to not show the remove button, but that \
requires too much effort, I am too lazy, this works!');
}
});
});
// Send server stats
setInterval(function() {
websock.sockets.in('users').emit('server stats', {
totalConnectedUsers: countTotalUsers(),
totalSongs: medialist.list.length
});
}, 2000);
playlist.onCurrentSongComplete = function(){
websock.sockets.in('users').emit('playlist updated', playlist.list);
};
function countTotalUsers() {
var count = 0;
var users = websock.sockets.in('users').manager.connected;
for (var u in users) {
count++;
}
return count;
}
console.log('Server started on port: ' + PORT);