将Javascript时钟与服务器时钟同步 [英] Synchronizing Javascript clock with server clock

查看:98
本文介绍了将Javascript时钟与服务器时钟同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我正在创建一个AJAX聊天系统。它看起来像这样:

I'm creating an AJAX chat system. It looks something like this:


Mike - hi Jane - 5分钟前

Mike - hi Jane - 5 minutes ago

- 嗨迈克 - 4分钟前

Jane - hi Mike - 4 minutes ago

迈克 - 怎么回事 - 3秒前

Mike - how's it going - 3 seconds ago

- 我做得好 - 1秒前

Jane - I'm doing good - 1 second ago

Javascript每分钟轮询服务器,服务器响应10最新的消息。附加到每个消息的是Unix时间戳(PHP time())。 Javascript负责将用户当前拥有的缓冲消息与此新服务器响应合并。消息的交错和X time ago消息的准确性取决于Javascript时钟与服务器时钟同步的程度。

Javascript polls the server every minute and the server responds with 10 of the newest messages. Attached to each message is a Unix timestamp (PHP time()). Javascript is responsible for merging the buffered messages the user currently has with this new server response. The interleaving of messages and accuracy of the "X time ago" message is dependent on how well the Javascript clock is synched with the server clock.

问题

如何你准确地将Javascript时钟与服务器时钟同步吗?以下是我到目前为止的情况。我想让它更准确。目前它没有考虑进行同步的Ajax的往返时间。我如何知道发送,处理和接收的往返比例?另外,你如何处理时钟晶体被破坏的用户 - 例如:格林威治标准时间下午7:20,但他们的时钟实际上是格林尼治标准时间下午7:15?

How do you accurately synch the Javascript clock with server clock? Below is what I have so far. I would like to make it more accurate. Currently it does not consider the roundtrip time of the Ajax which does the synching. How do I know the proportion of the roundtrip that is sending, processing, and receiving? Also, how do you deal with users who's clock crystals are just plain busted - for example: it's 7:20 pm GMT but their clock actually says 7:15 pm GMT?

Clock = {
   serverOffset = 0;

   synch: function() {
      new Ajax.Request(
         '/getServerTime.php', {
            onSuccess: function(transport) {
               this.serverOffset = parseInt(transport.responseText) - 
                  new Date().getTime() / 1000;
            }
         }
      );
   },

   getServerTime: function(myTime) {
      if (typeof(myTime) === 'undefined') {
         myTime = new Date().getTime() / 1000;
      }
      return myTime + this.serverOffset;
   },

   serverToMyTime: function(serverTime) {
      return serverTime - this.serverOffset;
   }
}


推荐答案

这里有正如我在此答案中所解释的那样,限制了您的精确程度/ a> - 实质上,任何小于请求往返时间的时钟偏差都是不可检测的。如果您确实需要时间尽可能精确,请为服务器创建一个特殊的请求类型,以便尽可能快地完成往返。

There's a limit to how precise you can be, as I explained in this answer -- in essence, any clock skew smaller than the request round-trip time is undetectable. If you really need the time to be as precise as possible, create a special request type to the server that just gets the time, and do whatever you can to make the round-trip as fast as possible.

您可以识别本地时钟明显疯狂的浏览器,因为观察到的服务器时间不会落在请求的本地开始和结束时间之间。由于你无法将其收紧得太远,只需在本地请求[start,end]间隔内选择一个任意时间,并将其视为等同于接收的服务器时间。

You can identify browsers whose local clocks are clearly insane, though, because the observed server time will not fall between the request's local start and end time. Since you can't tighten it down much farther than that, just pick an arbitrary time within the local request [start, end] interval and treat that as being equivalent to the received server time.

这篇关于将Javascript时钟与服务器时钟同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆