简体   繁体   中英

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. 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.

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM