forked from paulbooth/syncplay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
44 lines (38 loc) · 962 Bytes
/
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
/*
* This is the audio element for the page. We use this to play audio.
*/
var audio;
function init() {
initSocket();
initAudioElement();
}
function initSocket() {
var socket = new io.Socket(window.location.hostname, {port: 8080});
socket.connect();
socket.on('connect', function(evt) {
console.log(evt);
});
socket.on('message', function(evt) {
if ('src' in evt) {
audio.setAttribute('src', evt.src);
} else if ('play' in evt) {
setTimeout(play, evt.play);
} else if ('latencyTest' in evt) {
console.log('latencyTest');
console.log(evt.latencyTest);
socket.send(
{'latencyTest':
new Date().getTime() - evt.latencyTest
});
}
});
socket.on('disconnect', function() {
console.log('client disconnect');
});
}
function initAudioElement() {
audio = document.createElement('audio');
}
function play() {
audio.play();
}