Skip to content

Track server date via headers to avoid drifting. #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ if(typeof window!="undefined") {
var x=new XMLHttpRequest();
x.onreadystatechange=function() {
if(x.readyState !== XMLHttpRequest.DONE)return;
cb(null,{statusCode:x.status},x.responseText?JSON.parse(x.responseText):[]);
var headers=x.getAllResponseHeaders().trim().split('\r\n');
var map={}
for(var i=0;i<headers.length;++i) {
headers[i]=headers[i].trim().split(': ');
map[headers[i].shift()]=headers[i].join(': ');
}
cb(null,{statusCode:x.status,headers:map},x.responseText?JSON.parse(x.responseText):[]);
}

x.open(ops.method,ops.uri);
Expand All @@ -21,12 +27,15 @@ else {
}



var LAST_SERVER_DATE=-1;
var API= {
domain_root: "www.hackmud.com",
__promise_wrap:(endpoint,dat) => {
return new Promise( (resolve,reject) => {
request({ method: 'POST', uri: 'https://'+API.domain_root+'/mobile/'+endpoint+'.json', json:dat},
(error,response,body) => {
LAST_SERVER_DATE=new Date(response.headers.date)/1||-1;
if(!error && response.statusCode == 200)
resolve(body)
else {
Expand Down Expand Up @@ -124,7 +133,15 @@ Account.prototype.poll=function(ext={}) {
ext.before=this.last+0.1;
if(ext.after=='last') {
ext.after=this.last-0.1;
var five_min_ago=new Date()/1000 - 300;
var five_min_ago;
if(LAST_SERVER_DATE > 0) {
// if we have a last date from the server, we can use it to ensure we don't drift
five_min_ago=LAST_SERVER_DATE/1000 - 300;
}
else {
// reluctantly use local dates
five_min_ago=new Date()/1000 - 300;
}
if(ext.after < five_min_ago)
ext.after=five_min_ago
}
Expand Down