我正在找到一种方法来区分客户端时钟和服务器时钟.
直到现在我尝试了以下方法.
搜罗:
问题是我们在到达服务器的请求和到达客户端的响应之间会有未知的延迟.
这是使用JavaScript和PHP的这个方案的实现:
time.js
var request = new XMLHttpRequest();
request.onreadystatechange = readystatechangehandler;
request.open("POST", "http://www.example.com/sync.php", true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send("original=" + (new Date).getTime());
function readystatechangehandler() {
var returned = (new Date).getTime();
if (request.readyState === 4 && request.status === 200) {
var timestamp = request.responseText.split('|');
var original = + timestamp[0];
var receive = + timestamp[1];
var transmit = + timestamp[2];
var sending = receive - original;
var receiving = returned - transmit;
var roundtrip = sending + receiving;
var oneway = roundtrip / 2;
var difference = sending - oneway; // this is what you want
// so the server time will be client time + difference
}
}
Run Code Online (Sandbox Code Playgroud)
Sync.php
<?php
$receive = round(microtime(true) * 1000);
echo $_POST["original"] . '|';
echo $receive . '|';
echo round(microtime(true) * 1000);
?>
Run Code Online (Sandbox Code Playgroud)
即使采用这种方法,我也会得到50-500毫秒的错误.如果延迟很高,则误差会更大.
但我想知道一家名为"adtruth"的公司声称他们能够根据时钟时间区分设备.他们将其称为"时间差分链接"设备识别的关键AdTruth风格是其专利技术TDL,用于时间差分链接.虽然在数十亿个连接设备中可能有数千个具有相同配置,但没有两个设备的时钟设置为同一时间 - 至少,当你将其降低到毫秒时.第41参数和AdTruth的创始人Ori Eisen说:"我们采用这些不同的时间戳并将它们与服务器主时钟进行比较.如果有任何疑问,TDL就是决胜局."
http://www.admonsters.com/blog/adtruth-joins-w3c-qa-ori-eisen-founder-and-chief-innovation-officer
这是他们的"时间差异链接"专利的链接
看起来他们在描述中只是忽略了网络延迟的问题。我注意到他们的(含糊的)措辞:
(...) 基于匹配[时间增量]参数落在所选范围内的确定(...)
这可以解释网络延迟的变化。
在互联网等大型繁忙网络上,不可能将精度“降低到毫秒”。其他网络类型(我认为令牌环或具有非常非常严格的 QoS 策略的网络)可能允许这种精度级别。