简体   繁体   English

如何将 Amazon S3 存储桶中的 javascript 连接到 Amazon 时间服务器?

[英]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.

相关问题 Javascript从亚马逊s3存储桶下载文件? - Javascript to download a file from amazon s3 bucket? 仅使用javascript显示Amazon S3存储桶中的图像 - showing image from Amazon S3 bucket only with javascript 如何让我的 HTML 文件访问 Amazon S3 存储桶中的 CSS 和 JavaScript 文件? - How can I get my HTML file to access my CSS and JavaScript files in an Amazon S3 Bucket? 如何使用Node / Angular / JavaScript将多个图像上传到Amazon AWS S3存储桶中 - How to upload multiple images into amazon AWS S3 bucket using Node/Angular/JavaScript 如何上传到 Amazon S3 存储桶中的新文件夹? - How to upload to a new folder in Amazon S3 Bucket? 无法从位于 S3 存储桶中的 HTML 文件通过 Socket.io 连接或发送到 Amazon ECS Socket.io 服务器 - Cannot connect or emit to Amazon ECS Socket.io Server via Socket.io from a HTML file located in S3 bucket Amazon S3 存储桶策略公开 - Amazon S3 Bucket Policy Public 如何使用 node js 将 S3 连接到 Amazon Textract - How to Connect S3 to Amazon Textract using node js 在使用JavaScript SDK for Amazon S3创建存储桶到我的实时存储库时出错 - Getting error while using the javascript Sdk for amazon s3 to Create bucket to my live repository 如何将画布数据uri上传到amazon s3服务器 - How to upload canvas data uri to amazon s3 server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM