Skip to content

Instantly share code, notes, and snippets.

@makotom
Last active April 10, 2019 01:03
Show Gist options
  • Save makotom/11366520 to your computer and use it in GitHub Desktop.
Save makotom/11366520 to your computer and use it in GitHub Desktop.
Client implementation for JSON time service by ntp.nict.jp
(function(){
var SERVERS = [
"http://ntp-a1.nict.go.jp/cgi-bin/jsont",
"http://ntp-b1.nict.go.jp/cgi-bin/jsont"
],
http = require("http");
(function(){
var Server = function(serverName){
var responseOn = 0,
jsont = function(body){
return body;
},
fireServerReq = function(postrun){
return http.get(encodeURI(serverName + "?" + (new Date().getTime() / 1000).toString()), postrun);
},
ntpStat = function(){
process.stdout.write(serverRes.id + ": ");
process.stdout.write(new Date(serverRes.st * 1000) + ", ");
process.stdout.write("rtt " + (responseOn - serverRes.it).toString() + ", ");
process.stdout.write("diff " + (serverRes.st - serverRes.it - (responseOn - serverRes.it) / 2).toString() + "\n");
},
receptor = function(res){
var fullStr = "";
res.on("data", function(chunk){
fullStr += chunk.toString();
});
res.on("end", function(){
serverRes = eval(fullStr);
ntpStat();
});
};
this.sync = function(){
fireServerReq(receptor).on("response", function(){ responseOn = new Date().getTime() / 1000; });
};
};
SERVERS.forEach(function(server){
var thisSrv = new Server(server);
thisSrv.sync();
});
})();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment