[英]How to connect javascript in Amazon S3 bucket to Amazon time server?
I have a javascript project that is currently deployed in an Amazon S3 bucket.我有一个 javascript 项目,目前部署在 Amazon S3 存储桶中。 On the page, there is a clock that I would like to sync up to a time server to provide more accuracy.
在页面上,我想将时钟同步到时间服务器以提供更高的准确性。 I have code currently, to try and connect, but am not sure what to do to sync up to the time server correctly:
我目前有代码,可以尝试连接,但不确定如何正确同步到时间服务器:
try {
//FF, Opera, Safari, Chrome
var xmlHttp = new XMLHttpRequest();
xmlhttp.open("GET", "169.254.169.12", false);
xmlhttp.send();
var dateStr = xmlhttp.getResponseHeader('Date');
var serverTimeMillisGMT = Date.parse(new Date(Date.parse(dateStr)).toUTCString());
var localMillisUTC = Date.parse(new Date().toUTCString());
return serverTimeMillisGMT - localMillisUTC;
}
catch (err1) {
//IE
try {
var xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
xmlhttp.open("GET", "169.254.169.12", false);
xmlhttp.send();
var dateStr = xmlhttp.getResponseHeader('Date');
var serverTimeMillisGMT = Date.parse(new Date(Date.parse(dateStr)).toUTCString());
var localMillisUTC = Date.parse(new Date().toUTCString());
document.getElementById("serverTime").innerHTML = "This clock is synced to a time server.";
return serverTimeMillisGMT - localMillisUTC;
}
catch (err2) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.open("GET", "169.254.169.12", false);
xmlhttp.send();
var dateStr = xmlhttp.getResponseHeader('Date');
var serverTimeMillisGMT = Date.parse(new Date(Date.parse(dateStr)).toUTCString());
var localMillisUTC = Date.parse(new Date().toUTCString());
document.getElementById("serverTime").innerHTML = "This clock is synced to a time server.";
return serverTimeMillisGMT - localMillisUTC;
}
catch (err3) {
//use CPU time.
console.log("using machine time...");
document.getElementById("machineTime").innerHTML = "This clock is synced to machine time.";
return 0;
}
}
I currently have the IP address of the time server ( https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/windows-set-time.html#windows-configuring-ntp ), but that does not work.我目前有时间服务器的 IP 地址( https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/windows-set-time.html#windows-configuring-ntp ),但这不起作用。
Thoughts on how to sync with the Amazon Time Server?关于如何与亚马逊时间服务器同步的想法?
Thanks!谢谢!
IF the JavaScript is running on Amazon S3 then this will be being served as a front-end application (unlink NodeJS which is server-side).如果 JavaScript 在 Amazon S3 上运行,那么它将被用作前端应用程序(取消连接服务器端的 NodeJS)。
Without being able to call a backend you will be limited to the system time of the users local machine if you're looking for a purely frontend solution to your problem.如果您正在寻找一个纯粹的前端解决方案来解决您的问题,那么您将受限于用户本地计算机的系统时间,而无法调用后端。
If you wanted to sync using Amazons servers you would need to make use of a backend app which can get this time from server its running on rather than the users machine.如果您想使用亚马逊服务器进行同步,您需要使用一个后端应用程序,该应用程序可以从运行的服务器而不是用户机器上获取这个时间。
This would be possible most simply by setting up an API Gateway and Lambda combination, but you would need to be aware there will still be the latency of round trip of the users machine connecting to the API and then receiving the response.最简单的方法是设置API 网关和Lambda组合,但您需要注意,连接到 API 然后接收响应的用户机器仍然存在往返延迟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.