Skip to content

Instantly share code, notes, and snippets.

@glenjamin
Forked from makeusabrew/client.js
Created December 13, 2011 10:45

Revisions

  1. glenjamin revised this gist Dec 13, 2011. 1 changed file with 20 additions and 29 deletions.
    49 changes: 20 additions & 29 deletions client.js
    Original 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() {
    var Client = function(socket) {
    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);
    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;
  2. @makeusabrew makeusabrew created this gist Dec 13, 2011.
    38 changes: 38 additions & 0 deletions client.js
    Original 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;
    })();
    8 changes: 8 additions & 0 deletions usage.js
    Original 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
    });