Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,378
    Thanks
    11
    Thanked 592 Times in 572 Posts

    timing: how to determine lag?

    I've come across a simple yet rather interesting problem that i can't see a find simple solution to.

    I need to figure out how to exactly sync two clients, or at least determine the one-way lag between them.

    i have a music app that loads a list of http-view-able mps and plays them using an audio tag. i wanted to hear it around the house, so i wrote a lightweight shell client that talks to this music app, getting it's mp3 url and using it's own <audio> tag to play that same url on a different device.

    i am using a comet/event source server to broadcast events ;
    "client > server > all clients"


    The master player broadcasts it's <audio>.currentTime every 10 seconds for the clients to sync upon, but the lag between the mp3 server, the master player app, the event server, and each client is unpredictable.

    This makes the songs not exactly sync up, which is annoying when you're in a part of the house where two devices overlap. Even an 80ms discrepancy is annoying; it sounds like a parking garage. around 120-200ms it sounds bouncy, and longer delays are just plain annoying.


    if i broadcast the master position as 1m23s132ms, its 20-120 ms behind by the time the client get it. this lag can vary with each ping, so it needs to be computed on-the-fly each time.

    Theory: If i can determine how far apart each client's system clock is from the master app's clock, i can determine an EXACT offset by bundling a timestamp with each response, and subtracting the difference. that's the solution, i know it is...


    Question:
    How can one tell how far apart two clocks are, if one doesn't know how long it takes to check the time?

    for example, if it comes back that they are 150ms apart, are they basically synced and the trip took 149ms, or are they 100ms apart and the trip took 50ms? it feels like i don't have enough info to solve the equation.

    if it helps to visualize, the challenge is the same as determining what portion of a regular ajax request is spent getting to the server, and what portion is spent on the journey back. While it's easy to log the round-trip time, the half-leg distance is what's important, and 50% of the total is not correct in my tests...



    any suggestions or ideas? i'm VERY open here.

    this has stumped me for a couple weeks, please help. pretty please, with &%^# sugar on top!
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,378
    Thanks
    11
    Thanked 592 Times in 572 Posts
    ok, i got it down to about +-20ms using a simple ajax routine.

    i was able to do this because i noticed that in chrome's inspector's Net tab that my connections were 98% lag. that is to say that most of the round-trip time of an ajax request is spent reaching the server, at which point it returns to my browser in 2ms.

    so if you'r net tab looks like this:
    ####################

    instead of this
    ####################

    you can do the same.

    here i make a request the first time, grab the dealye, and make an Date emulator so that i can compare my local estimate to the server, which is what happens on all executions except the first:


    Code:
    if(window.dt){
      aGet(location.href, function(e) {
    	var serverTime= (e * 1)+2 ;
        console.log( serverTime - dt()   );
      });
    }else{
      aGet(location.href, function(e) {
    	var n = +new Date,  serverTime= (e * 1)+2 ;
       var off= serverTime - n ;
        window.dt=function(){return +new Date() + off; };
        console.log( off );
      });
    }
    window.dt here is like Date.now(), but returns the date on the server instead of the local time.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •