-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
210 lines (146 loc) · 5.44 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
var express = require('express');
var socketio = require('socket.io');
var scApi = require('soundclouder');
var config = require('config');
var bodyParser = require('body-parser');
var passport = require('passport');
var SoundCloudStrategy = require('passport-soundcloud').Strategy;
var app = express();
//app.use(express.logger());
//app.use(express.cookieParser());
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(__dirname + '/public'));
// session setup
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
var currentTrackId;
var currentTrack;
var songPlayTime = {};
var songQueue = [];
var songEndTimer;
var server = app.listen(3000);
console.log("server started on port 3000");
var scConfig = config.get('SoundCloud');
// configure from config file
scApi.init(scConfig.clientId, scConfig.clientSecret, scConfig.redirectUrl);
var io = socketio.listen(server);
io.on('connection', function(socket) {
console.log('client connected');
if (currentTrackId) {
var currentTime = (new Date()).getTime();
var songPlayedTime = songPlayTime[currentTrackId];
var playedTime = currentTime - songPlayedTime;
console.log("currentTime " + currentTime + ", songPlayedtime " + songPlayedTime + ", playedTime " + playedTime + ", song duration " + currentTrack.duration);
console.log("sending song " + currentTrackId + " to newly connected client");
if (playedTime < currentTrack.duration) {
currentTrack.position = playedTime;
socket.emit('new song', currentTrack);
//res.json(currentTrack);
}
}
});
passport.use(new SoundCloudStrategy({
clientID: scConfig.clientId,
clientSecret: scConfig.clientSecret,
callbackURL: scConfig.redirectUrl
}, function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
return done(null, profile);
});
}
));
app.get('/auth/soundcloud',
passport.authenticate('soundcloud'),
function(req, res){
// The request will be redirected to SoundCloud for authentication, so this
// function will not be called.
});
app.get('/auth/soundcloud/callback',
passport.authenticate('soundcloud', { failureRedirect: '/login.html' }),
function(req, res) {
res.redirect('/');
}
);
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login.html');
}
app.get('/login', function(req, res){
res.render('login', { user: req.user });
});
app.get('/api/current', function(req, res, next) {
//socket.broadcast.emit('server ready', { 'message' : 'ok' });
if (currentTrackId) {
var currentTime = (new Date()).getTime();
var songPlayedTime = songPlayTime[currentTrackId];
var playedTime = currentTime - songPlayedTime;
console.log("currentTime " + currentTime + ", songPlayedtime " + songPlayedTime + ", playedTime " + playedTime + ", song duration " + currentTrack.duration);
console.log("sending song " + currentTrackId + " to newly connected client");
if (playedTime < currentTrack.duration) {
//socket.emit('new song', { 'trackId' : currentTrackId, 'position' : playedTime, 'title' : currentTrack.title });
currentTrack.position = playedTime;
res.json(currentTrack);
}
}
});
app.get('/api/queue', function(req, res, next) {
res.json(songQueue);
});
app.get('/api/skip', function(req, res, next) {
currentTrackId = null;
currentTrack = null;
if (songQueue.length > 0) {
console.log("Playing next track that's enqueued");
playNextTrack();
res.json({status: 'skipped to next song'});
} else {
// stop current track
io.sockets.emit('stop current', {});
res.json({status: 'stopping current song, none to move to'});
}
});
app.put('/api/request', function(req, res, next) {
var track = req.body;
var trackId = req.body.id;
// if no songs in queue
if (!currentTrack) {
songQueue.push(track);
playNextTrack();
res.json({ status: "success"});
} else if (songQueue.length > 50) {
res.json({ status: "queue full"});
} else {
// add to queue
songQueue.push(track);
io.sockets.emit('update queue', songQueue);
res.json({ status: "song added"});
}
});
function playNextTrack() {
var nextTrack = songQueue.shift();
io.sockets.emit('update queue', songQueue);
// then play
clearTimeout(songEndTimer);
var currentTime = (new Date()).getTime();
currentTrackId = nextTrack.id;
currentTrack = nextTrack;
songPlayTime[currentTrackId] = currentTime;
songEndTimer = setTimeout(function() {
currentTrackId = null;
currentTrack = null;
if (songQueue.length > 0) {
console.log("Playing next enqueued track");
playNextTrack();
}
}, nextTrack.duration);
console.log("Start playing trackId " + currentTrackId + " at " + currentTime);
io.sockets.emit('new song', nextTrack);
}