Permalink
Browse files

use cristian's algorithm for clock syncing instead of latency tests.

  • Loading branch information...
1 parent 3b9a69d commit 6b713c647e03fa8ce41f587c68297e4c3d74bfc0 @noahlt committed Jul 31, 2011
Showing with 153 additions and 98 deletions.
  1. +1 −0 .gitignore
  2. +5 −4 index.html
  3. +50 −52 main.js
  4. +97 −42 server.js
View
@@ -1,3 +1,4 @@
*~
#*#
.idea
+node_modules
View
@@ -2,7 +2,7 @@
<head>
<title>Audio test</title>
- <script src="/socket.io/socket.io.js"></script>
+ <script src="socket.io/socket.io.js"></script>
<script src="jquery.min.js"></script>
<script src="main.js"></script>
<style>
@@ -11,9 +11,10 @@
</style>
</head>
-<body onload="init();">
- <h1> THIS IS A TRRST.</h1>
- <p> do not touch the glass.
+<body>
+ <h1>THIS IS A TEST</h1>
+ <p>do not touch the glass.</p>
+ <button id="play">Play</button>
</body>
</html>
View
@@ -1,58 +1,56 @@
-/*
- * This is the audio element for the page. We use this to play audio.
- */
-var audio;
-var socket;
-var startTime;
-var offset;
-function init() {
- initSocket();
- initAudioElement();
-}
+// Utilities
-function initSocket() {
- socket = new io.Socket(window.location.hostname, {port: 8080});
-
- socket.connect();
-
- socket.on('connect', function(evt) {
- console.log(evt);
- syncClocks();
- });
- 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
- });
- } else if ('serverTime' in evt) {
- if (startTime == null) {
- throw "clock sync not run correctly upon server connection, please try again?"
- }
- var serverTime = evt.serverTime;
- var oneWayTime = ((new Date()).getTime() - startTime)/2;
- offset = serverTime - (oneWayTime + startTime)
- }
- });
- socket.on('disconnect', function() {
- console.log('client disconnect');
- });
+function now() {
+ return (new Date()).getTime();
}
-function initAudioElement() {
- audio = document.createElement('audio');
+function at(datetime) {
+ return datetime - now();
}
-function play() {
- audio.play();
-}
+// Init
-function syncClocks(){
- startTime = (new Date()).getTime();
-}
+$(function() {
+ console.log('starting init...');
+ var audioElement, socket, startTime, offset;
+
+ audioElement = document.createElement('audio');
+
+ console.log(1);
+ socket = new io.connect('http://' + window.location.hostname + ':8080');
+ console.log(window.location.hostname);
+
+ socket.on('connect', function(data) {
+ console.log('connected; starting clock sync');
+ startTime = now();
+ socket.send({'startClockSync': true});
+ });
+
+ socket.on('message', function(data) {
+ if ('src' in data) {
+ console.log('audio src received');
+ audioElement.setAttribute('src', data.src);
+ } else if ('play' in data) {
+ console.log('received play message');
+ setTimeout(function() { audioElement.play(); },
+ at(data.play + offset));
+ } else if ('clockSyncServerTime' in data) {
+ console.log('server clock sync received; setting offset');
+ if (startTime == null) {
+ throw "clock sync failed: startTime didn't get set.";
+ }
+ var serverTime = data.clockSyncServerTime,
+ oneWayTime = (now() - startTime)/2;
+ offset = serverTime - (oneWayTime + startTime);
+ }
+ });
+
+ socket.on('disconnect', function() {
+ console.log('client disconnect');
+ });
+
+ $('#play').click(function() {
+ console.log('sending play message');
+ socket.send({'start': true});
+ });
+});
View
@@ -3,48 +3,103 @@ io = require('socket.io'),
path = require('path'),
paperboy = require('paperboy');
-var audioSrc = 'audio/test.mp3';
-var startTimeMax = 5000;
-var startTime = startTimeMax;
-var startTimeout;
-var countDownTimeout;
-var clients = [];
-var server = http.createServer(function(req, res) {
- paperboy.deliver(path.dirname(__filename), req, res);
- });
+var getNetworkIP = (function () {
+ var ignoreRE = /^(127\.0\.0\.1|::1|fe80(:1)?::1(%.*)?)$/i;
+
+ var exec = require('child_process').exec;
+ var cached;
+ var command;
+ var filterRE;
+
+ switch (process.platform) {
+ // TODO: implement for OSs without ifconfig command
+ case 'darwin':
+ command = 'ifconfig';
+ filterRE = /\binet\s+([^\s]+)/g;
+ // filterRE = /\binet6\s+([^\s]+)/g; // IPv6
+ break;
+ default:
+ command = 'ifconfig';
+ filterRE = /\binet\b[^:]+:\s*([^\s]+)/g;
+ // filterRE = /\binet6[^:]+:\s*([^\s]+)/g; // IPv6
+ break;
+ }
-server.listen(8080, '192.168.2.4');
-
-var socket = io.listen(server);
-socket.on('connection', function(client) {
- client.send({'src': audioSrc});
- clients.push(client);
- startTime = startTimeMax;
- clearTimeout(countDownTimeout);
- client.send({'latencyTest': new Date().getTime()});
- startCountDown();
- client.on('message', function(data) {
- if ('latencyTest' in data) {
- client.latency = data.latencyTest;
- }
- });
- });
-function start() {
- var date = new Date();
- var curtime = date.getTime();
- var goTime = 5000 + curtime;
- for (var i = 0; i < clients.length; i++) {
- clients[i].send({'play': goTime - new Date().getTime() -
- clients[i].latencyTest});
+ return function (callback, bypassCache) {
+ // get cached value
+ if (cached && !bypassCache) {
+ callback(null, cached);
+ return;
}
-}
-console.log('awesome. it\'s up.');
-function startCountDown() {
- startTime -= 1000;
- console.log('t:'+startTime);
- if (startTime > 0) {
- countDownTimeout = setTimeout(startCountDown, 1000);
- } else {
+ // system call
+ exec(command, function (error, stdout, sterr) {
+ var ips = [];
+ // extract IPs
+ var matches = stdout.match(filterRE);
+ // JS has no lookbehind REs, so we need a trick
+ for (var i = 0; i < matches.length; i++) {
+ ips.push(matches[i].replace(filterRE, '$1'));
+ }
+
+ // filter BS
+ for (var i = 0, l = ips.length; i < l; i++) {
+ if (!ignoreRE.test(ips[i])) {
+ //if (!error) {
+ cached = ips[i];
+ //}
+ callback(error, ips[i]);
+ return;
+ }
+ }
+ // nothing found
+ callback(error, null);
+ });
+ };
+})();
+
+getNetworkIP(function (error, ip) {
+ console.log(ip);
+ if (error) {
+ console.log('error:', error);
+ } else {
+ runServer(ip);
+ }
+}, true);
+
+function runServer(ip) {
+ console.log('ip = ', ip);
+ var audioSrc = 'audio/test.mp3';
+ var clients = [];
+ var server = http.createServer(function(req, res) {
+ paperboy.deliver(path.dirname(__filename), req, res);
+ });
+
+ server.listen(8080);
+
+ var socket = io.listen(server);
+ socket.on('connection', function(client) {
+ console.log('client connect.');
+ client.send({'src': audioSrc});
+ clients.push(client);
+ client.on('message', function(data) {
+ if ('startClockSync' in data) {
+ console.log('syncing clock');
+ client.send({'clockSyncServerTime': (new Date()).getTime()});
+ } else if ('start' in data) {
+ console.log('playing music');
start();
- }
-}
+ }
+ });
+ });
+
+ function seconds(n) {
+ return n * 1000;
+ }
+
+ function start() {
+ clients.forEach(function(client) {
+ client.send({'play': (new Date()).getTime() + seconds(5)});
+ });
+ }
+ console.log('awesome. it\'s up.');
+}

0 comments on commit 6b713c6

Please sign in to comment.