views:

48

answers:

1

My server broadcasts a packet to a browser client every now and then.

Is there any way to figure out how long it took from the packet from:

server --> client

I'm trying to time something, so ideally my JavaScript function would be like:

var travelTime = {amount of time it took packet to get to me};
setTimeout('myFunction()', 3000 + travelTime);

Also, I'm not requesting this packet so I can't log the send time, and log the recieve time on the client side. This packet request is not bi-directional, as I establish a socket with the server and the server has the ability to just fire me a packet.

Any advice is appriciated!

EDIT: comet-style connection

+2  A: 

You could include a timestamp in the packet, but that would depend on both systems being synchronized to the same clock (using NTP, for instance). If you don't have control of both server and client then what you are asking is impossible without a round-trip. Even with NTP synchronization there will be some jitter depending on how often the clocks synch. With a round trip your latency calculation will only be an estimate anyway, subject to serious deviations as network conditions change unless both client and server are on the same LAN.

Jim Garrison