-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
315 lines (252 loc) · 8.46 KB
/
index.html
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<html>
<head>
<script src='https://cdn.firebase.com/v0/firebase.js'></script>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js'></script>
</head>
<body>
<div id='roomDiv'>
<input type='text' id='nameInput' placeholder='Name'>
<input type='text' id='roomInput' placeholder='Room'>
</div>
<div id='clientDiv'>
<div id='queueDiv'>
<div id='queueContainer'>
<div id='tracksDiv'></div>
</div>
<input type='text' id='trackInput' placeholder='URL here'>
</div>
<div id='chatDiv'>
<div id='messageContainer'>
<div id='messagesDiv'></div>
</div>
<input type='text' id='messageInput' placeholder='Message'>
</div>
<div id='usersDiv'></div>
<br><br>
<div id='playerDiv'>
</div>
</div>
<script>
$('#clientDiv').hide();
$('#roomInput').keypress(function (e) {
if (e.keyCode == 13) {
var room = $('#roomInput').val();
var name = $('#nameInput').val();
joinRoom( room, name );
$('#roomInput').val('');
}
});
var userList = {};
var trackQueue = {};
var isCoordinator = false;
var currentTrack = null;
var currentTrackDuration = 0;
var currentPlayTime = 0;
var isPlaying = false;
var currentCoordDuration = 0;
function joinRoom( room, name )
{
$('#roomDiv').hide(1000);
$('#clientDiv').show(1000);
var users = new Firebase('https://officeradio.firebaseio.com/'+room+'/users/');
var chat = new Firebase('https://officeradio.firebaseio.com/'+room+'/chat/');
var queue = new Firebase('https://officeradio.firebaseio.com/'+room+'/queue/');
var track = new Firebase('https://officeradio.firebaseio.com/'+room+'/track/');
var coord = new Firebase('https://officeradio.firebaseio.com/'+room+'/coordinator/');
var play = new Firebase('https://officeradio.firebaseio.com/'+room+'/play/');
setupChat( chat, name );
setupUsers( users );
setupQueueMaintenance( queue );
chat.push({name: name, join : true});
var user = users.push({name: name});
var playUpdate = play.on('value', function(snapshot){
currentPlayTime = snapshot.val();
})
var coordUpdate = coord.on('value', function(snapshot) {
if (snapshot.val() == null)
{
coord.transaction(function(){coord.set(user.toString());});
}
else
{
if (snapshot.val() == user.toString())
{
setupCoordinator( user, coord, play, track );
}
var name;
new Firebase(snapshot.val()).once('value', function(v){ name = v.val().name; });
if (userList[name] != null) userList[name].addClass('coordinator');
}
})
track.on('value', function(snapshot) {
currentTrack = snapshot.val();
if (currentTrack != null)
{
// play here
var trackData;
new Firebase(snapshot.val()).once('value', function(v){ trackData = v.val(); });
var playStart = currentPlayTime;
currentTrackDuration = trackData.youtube.duration;
if (playStart != 0) playStart += 1; // compensate for starting up the video player
loadYoutube( trackData.youtube.ytid, playStart );
}
else
{
clearPlayer();
if (isCoordinator)
{
queue.startAt().limit(1).once("child_added", function(snapshot) {
play.set(0);
track.set(snapshot.ref().toString());
});
}
}
});
window.onbeforeunload = function() {
coord.off('value', coordUpdate);
if (isCoordinator) coord.set(null);
user.remove();
chat.push({name: name, leave : true});
}
}
function clearPlayer( )
{
$('#playerDiv').html('');
isPlaying = false;
}
function loadYoutube( ytid, start )
{
var html = '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/'+ytid+'&iv_load_policy=3&autoplay=1&showinfo=0&volume=100&start='+start+'"></param><param name="allowscriptaccess" value="always"></param><param name="volume" value="100"></param><param name="wmode" value="transparent"></param></param><embed src="http://www.youtube.com/v/'+ytid+'&iv_load_policy=3&autoplay=1&showinfo=0&volume=100&start='+start+'" type="application/x-shockwave-flash" allowscriptaccess="always" wmode="transparent" width="425" height="355" volume="100"></embed></object>'
$('#playerDiv').html(html);
isPlaying = true;
if (isCoordinator) currentCoordDuration = start;
}
function setupCoordinator( user, coord, play, track )
{
// when I'm quitting, stop me being coordinator
isCoordinator = true;
var tick = setInterval(function(){
if (isPlaying)
{
currentCoordDuration++;
play.set( currentCoordDuration );
if (currentCoordDuration >= currentTrackDuration && currentTrack != null)
{
new Firebase(currentTrack).remove();
track.set(null);
}
}
},1000)
}
function ytGetID( url )
{
var video_id = url.split('v=')[1];
var ampersandPosition = video_id.indexOf('&');
if(ampersandPosition != -1) {
video_id = video_id.substring(0, ampersandPosition);
}
return video_id;
}
function ytPushTrack( ytUrl, queue )
{
var video_id = ytGetID( ytUrl );
$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?v=2&alt=jsonc',function(data,status,xhr){
var data = {
url : ytUrl,
youtube :
{
ytid : video_id,
title : data.data.title,
thumb : data.data.thumbnail.hqDefault,
player : data.data.player.default,
duration : data.data.duration
}
};
queue.push(data);
});
}
function setupQueueMaintenance( queue )
{
$('#trackInput').keypress(function (e) {
if (e.keyCode == 13) {
var text = $('#trackInput').val();
ytPushTrack( text, queue );
$('#trackInput').val('');
}
});
queue.on('child_added', function(snapshot) {
var track = snapshot.val();
var uniqueID = snapshot.ref().name();
trackQueue[uniqueID] = $('<div/>').addClass('track').text(track.url);
trackQueue[uniqueID].appendTo($('#tracksDiv'));
});
queue.on('child_removed', function(snapshot) {
var track = snapshot.val();
var uniqueID = snapshot.ref().name();
trackQueue[uniqueID].remove();
trackQueue[uniqueID] = null;
});
}
var userList = {};
function setupUsers( users )
{
users.on('child_added', function(snapshot) {
var user = snapshot.val();
userList[user.name] = $('<div/>').addClass('user').text(user.name);
userList[user.name].appendTo($('#usersDiv'));
});
users.on('child_removed', function(snapshot) {
var user = snapshot.val();
userList[user.name].remove();
userList[user.name] = null;
});
}
function setupChat( chat, name )
{
$('#messageInput').keypress(function (e) {
if (e.keyCode == 13) {
var text = $('#messageInput').val();
chat.push({name: name, text: text});
$('#messageInput').val('');
}
});
chat.on('child_added', function(snapshot) {
var message = snapshot.val();
if (message.join)
{
displayChatMessage(message.name, 'joins', 'join');
}
else if (message.leave)
{
displayChatMessage(message.name, 'leaves', 'leave');
}
else
{
displayChatMessage(message.name, message.text, 'msg');
}
});
}
function displayChatMessage(name, text, cssClass) {
$('<div/>').addClass(cssClass).text(text).prepend($('<em/>').text(name+': ')).appendTo($('#messagesDiv'));
$('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;
}
/*var myDataRef = new Firebase('https://rj8mzygh5ob.firebaseio-demo.com/');
$('#messageInput').keypress(function (e) {
if (e.keyCode == 13) {
var name = $('#nameInput').val();
var text = $('#messageInput').val();
myDataRef.push({name: name, text: text});
$('#messageInput').val('');
}
});
myDataRef.on('child_added', function(snapshot) {
var message = snapshot.val();
displayChatMessage(message.name, message.text);
});
function displayChatMessage(name, text) {
$('<div/>').text(text).prepend($('<em/>').text(name+': ')).appendTo($('#messagesDiv'));
$('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;
};*/
</script>
</body>
</html>