-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
70 lines (62 loc) · 1.67 KB
/
main.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
// Utilities
var DEBUG = true;
function print(entry) {
return log(entry);
}
function log(entry) {
if (DEBUG) {
console.log(entry);
}
}
function now() {
return (new Date()).getTime();
}
function at(datetime) {
return datetime - now();
}
// Init
$(function() {
log('starting init...');
var audioElement, socket, startTime, offset;
audioElement = document.createElement('audio');
socket = io.connect();//'http://' + window.location.hostname + ':8080');
log(window.location.hostname);
socket.on('connect', function(data) {
log('connected; starting clock sync');
startTime = now();
socket.json.send({'startClockSync': true});
});
socket.on('message', function(data) {
if ('src' in data) {
log('audio src received:' + data.src);
audioElement.setAttribute('src', data.src);
audioElement.load();
} else if ('play' in data) {
log('received play message');
setTimeout(function() { audioElement.play(); },
at(data.play + offset));
} else if ('clockSyncServerTime' in data) {
log('server clock sync received; setting offset');
if (startTime == null) {
throw "clock sync failed: startTime didn't get set.";
}
offset = data.clockSyncServerTime - now()/2 + startTime/2;
}
});
socket.on('stop', function(data) {
log('stopping');
audioElement.stop();
window.location.href = window.location.href;
});
socket.on('disconnect', function() {
log('client disconnect');
});
$('#play').click(function() {
log('sending play message');
socket.json.send({'start': true});
});
$('#stop').click(function() {
log('sending stop message');
socket.emit('stopall');
});
});