Skip to content

Instantly share code, notes, and snippets.

@tacryt-socryp
Last active May 22, 2018 23:21
Show Gist options
  • Save tacryt-socryp/5c7692de806eced59422824124ead6b3 to your computer and use it in GitHub Desktop.
Save tacryt-socryp/5c7692de806eced59422824124ead6b3 to your computer and use it in GitHub Desktop.
var fetch = require('node-fetch');
function authenticate(code, success) {
fetch(`http://localhost:8080/~/auth.json`).then(res => res.json())
.then((json) => {
let payload = {
method: "PUT",
body: JSON.stringify({
ship: json.ship,
code: code,
oryx: json.oryx
})
};
let innerSuccess = (json) => {
console.log(json);
success(json);
};
fetch(`http://localhost:8080/~/auth.json`, payload).then(res => res.json()).then(innerSuccess).catch((error) => {
console.log(error);
});
});
}
function longPoll(seqn = 1, ixor) {
let success = (data) => {
let isBeat = false;
let newSeqn = seqn;
console.log(data);
if (data.beat) {
console.log('beat', seqn)
isBeat = true;
} else if ('data' in data && 'json' in data.data) {
console.log('new server data', data.data.json, data.from);
newSeqn = ++seqn;
}
this.longPoll(newSeqn);
};
fetch(`http://localhost:8080/~/of/${ixor}?poll=${seqn}&t=1`, {}).then(res => res.json()).then(success).catch((error) => {
console.log(error);
});
}
authenticate("socdex-ralner-bissel-milfyr", (json) => {
longPoll(1, json.ixor);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment