您如何确定发送HTTP请求的网络延迟的时间? [英] How do you determine exactly when, accounting for network latency, a HTTP request was sent?

查看:2204
本文介绍了您如何确定发送HTTP请求的网络延迟的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建应用程序,重要的是要知道用户何时向服务器提交请求。这与服务器接收到请求的时间不同,因为在用户按提交的时刻和服务器接收到的时间之间存在时间间隔(通常是网络流量和小区接收的变化) POST请求。

I am building an application where it is important to know exactly when a user submitted a request to the server. This is different from the time at which the server received the request, because there is an interval of time -- that is often subject to the vagaries of network traffic and cell reception -- between the moment the user presses submit and the server receives the POST request.

这个互动是通过一个使用JavaScript的Web浏览器进行的,所以任何涉及JavaScript的解决方案都是可以接受的。我想到使用JavaScript在用户的计算机或智能手机上获取当前时间,并将其用作提交的请求的时间戳,但由于用户计算机上的时钟可能关闭,因此本身不会是可靠的。请假设服务器上的时钟是准确的。

The interaction is through a web browser with JavaScript, so any solution that involves JavaScript is acceptable. I thought about getting current time on the user's computer or smartphone using JavaScript and using that as a timestamp on the submitted request, but that by itself wouldn't be reliable because the clock on the user's computer could be off. Please assume that the clock on the server is accurate.

推荐答案

我不认为可以得到一个确切的值。我认为你可以做的最好的是发送一个非常小的校准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.

这篇关于您如何确定发送HTTP请求的网络延迟的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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