Revisions
-
glenjamin revised this gist
Dec 13, 2011 . 1 changed file with 20 additions and 29 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,37 +1,28 @@ /** * Assume our client object manages all communication with the server and other wonderful stuff */ var Client = function(socket) { var that = {}; that.ping = function(limit, callback) { var results = []; return _ping(1, results, limit, callback); }; function _ping(iteration, results, limit, callback) { var sent = Date.now(); socket.emit('ping', function(timestamp) { var latency = (Date.now() - sent) / 2; results.push({ "latency": latency, "timestamp": timestamp }); if (iteration < limit) { return _ping(iteration + 1, results, limit, callback); } else { return callback(results); } }) } return that; -
makeusabrew created this gist
Dec 13, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ /** * Assume our client object manages all communication with the server and other wonderful stuff */ var Client = function() { var that = {}; that.ping = function(limit, cb) { var results = []; (function _doPing(iteration) { var sent = new Date().getTime(); // ping the server - we always expect just a timestamp in return socket.emit('ping', function(timestamp) { // take a best-guess at latency based on half the round trip var latency = (new Date().getTime() - sent) / 2; // let's store both the returned timestamp and latency for completeness results.push({ "latency": latency, "timestamp": timestamp }); // check if we need to go again or if we've got enough results if (iteration < limit) { _doPing(iteration+1); } else { cb(results); } }); // always execute the function on iteration 1. Yeah, non zero indexed, but hey... })(1); } return that; })(); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ Client.ping(10, function(results) { // results is an array of 10 objects for us to inspect and average out etc. console.log(results); }); Client.ping(1, function(result) { // result is a single array, useful to get current server timestamp etc });