且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

您如何确定发送HTTP请求的网络延迟的时间?

更新时间:2022-05-05 05:51:05

我不认为可以得到一个确切的值。我认为你可以做的***的是发送一个非常小的校准AJAX请求,等同于服务器时间的客户端时间。

I don't think it's possible to get an exact value. I think the best you can do is send a very small calibration AJAX request that equivalents the client time with the server time. That's what I've done anyhow.

编辑

基本上做对具有客户端时间戳(时钟)的服务器端点的AJAX调用。一旦服务器收到客户端时间戳,就可以检索并存储服务器时间戳。在这一点上,你可以假设时间是相对等价的。你不会知道网络开销,所以他们将不同;他们的时钟也可能在不同的时区,或者只是关闭。

Basically make an AJAX call to a server endpoint with the client timestamp (clock). As soon as the server receives the client timestamp retrieve and store the server timestamp along with it. At this point you can assume the times are relatively equivalent. You won't know the network overhead, so they will be different; also their clock could be in a different time zone or just "off".

在你有这个等价之后,你可以使用两次的差异校准任何其他时间客户端:

After you have this equivalence you can use the difference of the two times to calibrate any other times from the client:


  • clientTime0 - serverTime0 = clientServerDelta

  • (clientTimeX - clentServerDelta)将 about 在serverTimeX记录时与服务器时间相同。

  • clientTime0 - serverTime0 = clientServerDelta
  • (clientTimeX - clentServerDelta) will be about the same in server time as when clientTimeX was recorded.