mirror of
https://github.com/amark/gun.git
synced 2025-06-23 06:22:32 +00:00
Merge branch 'manhattan' of http://github.com/amark/gun into manhattan
This commit is contained in:
commit
1fb7088160
112
nts.js
112
nts.js
@ -1,40 +1,80 @@
|
|||||||
;(function(){
|
;(function(){
|
||||||
var Gun = (typeof window !== "undefined")? window.Gun : require('./gun');
|
var Gun = (typeof window !== "undefined")? window.Gun : require('./gun');
|
||||||
|
var dam = 'nts';
|
||||||
|
var smooth = 2;
|
||||||
|
|
||||||
Gun.on('create', function(root){ // switch to DAM, deprecated old
|
Gun.on('create', function(root){ // switch to DAM, deprecated old
|
||||||
var opt = root.opt, mesh = opt.mesh;
|
var opt = root.opt, mesh = opt.mesh;
|
||||||
if(!mesh){ return }
|
if(!mesh) return;
|
||||||
var asks = {};
|
|
||||||
mesh.hear['nts'] = function(msg, peer){
|
// Track connections
|
||||||
if(msg.nts){
|
var connections = [];
|
||||||
(asks[msg['@']]||noop)(msg);
|
root.on('hi', function(peer) {
|
||||||
return;
|
this.to.next(peer);
|
||||||
}
|
connections.push({peer, latency: 0, offset: 0, next: 0});
|
||||||
mesh.say({dam: 'nts', nts: Gun.state(), '@': msg['#']}, peer);
|
});
|
||||||
}
|
root.on('bye', function(peer) {
|
||||||
var peers = 0;
|
this.to.next(peer);
|
||||||
root.on('hi', function(peer){ this.to.next(peer);
|
var found = connections.find(connection => connection.peer.id == peer.id);
|
||||||
peers++;
|
if (!found) return;
|
||||||
setTimeout(function ping(){
|
connections.splice(connections.indexOf(found), 1);
|
||||||
var NTS = {}, ack = String.random(3), msg = {'#': ack, dam: 'nts'};
|
});
|
||||||
NTS.start = Gun.state();
|
|
||||||
asks[ack] = function(msg){
|
function response(msg, connection) {
|
||||||
NTS.end = Gun.state();
|
var now = Date.now(); // Lack of drift intentional, provides more accurate RTT
|
||||||
delete asks[ack];
|
connection.latency = (now - msg.nts[0]) / 2;
|
||||||
NTS.latency = (NTS.end - NTS.start)/2;
|
connection.offset = (msg.nts[1] + connection.latency) - (now + Gun.state.drift);
|
||||||
if(!msg.nts){ return }
|
console.log(connection.offset);
|
||||||
NTS.calc = NTS.latency + msg.nts;
|
Gun.state.drift += connection.offset / (connections.length + smooth);
|
||||||
NTS.step = (NTS.end - NTS.calc)/2;
|
console.log(`Update time by local: ${connection.offset} / ${connections.length + smooth}`);
|
||||||
Gun.state.drift -= NTS.step * (1/(peers||1));
|
}
|
||||||
NTS.next = Math.min(2e4, Math.max(250, 150000 / Math.abs((NTS.end - NTS.calc)||1)));
|
|
||||||
console.log("I am now", Gun.state(), "they are", NTS.calc, "time sync in", NTS.next/1000, 'sec.');
|
// Handle echo & setting based on known connection latency as well
|
||||||
setTimeout(ping, NTS.next); // Thanks @finwo ! https://discord.com/channels/612645357850984470/612645357850984473/755334349699809300
|
mesh.hear[dam] = function(msg, peer) {
|
||||||
}
|
console.log('MSG', msg);
|
||||||
mesh.say(msg, peer);
|
var now = Date.now() + Gun.state.drift;
|
||||||
}, 1);
|
var connection = connections.find(connection => connection.peer.id == peer.id);
|
||||||
});
|
if (!connection) return;
|
||||||
root.on('bye', function(peer){ --peers; this.to.next(peer) });
|
if (msg.nts.length >= 2) return response(msg, connection);
|
||||||
});
|
mesh.say({dam, '@': msg['#'], nts: msg.nts.concat(now)}, peer);
|
||||||
|
connection.offset = msg.nts[0] + connection.latency - now;
|
||||||
|
Gun.state.drift += connection.offset / (connections.length + smooth);
|
||||||
|
console.log(`Update time by remote: ${connection.offset} / ${connections.length + smooth}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Handle ping transmission
|
||||||
|
setTimeout(function trigger() {
|
||||||
|
console.log('TRIGGER');
|
||||||
|
if (!connections.length) return setTimeout(trigger, 100);
|
||||||
|
var now = Date.now(); // Lack of drift intentional, provides more accurate RTT & NTP reference
|
||||||
|
|
||||||
|
// Send pings
|
||||||
|
connections.forEach(function(connection) {
|
||||||
|
if (connection.next > now) return;
|
||||||
|
mesh.say({
|
||||||
|
dam,
|
||||||
|
'#': String.random(3),
|
||||||
|
nts: [now],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Plan next round of pings
|
||||||
|
connections.forEach(function(connection) {
|
||||||
|
if (connection.next > now) return;
|
||||||
|
// https://discord.com/channels/612645357850984470/612645357850984473/755334349699809300
|
||||||
|
var delay = Math.min(2e4, Math.max(250, 150000 / Math.abs((connection.offset)||1)));
|
||||||
|
connection.next = now + delay;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Plan next trigger round
|
||||||
|
// May overshoot by runtime of this function
|
||||||
|
var nextRound = Infinity;
|
||||||
|
connections.forEach(function(connection) {
|
||||||
|
nextRound = Math.min(nextRound, connection.next);
|
||||||
|
});
|
||||||
|
setTimeout(trigger, nextRound - now);
|
||||||
|
console.log(`Next sync round in ${(nextRound - now) / 1000} seconds`);
|
||||||
|
}, 1);
|
||||||
|
});
|
||||||
|
|
||||||
// test by opening up examples/game/nts.html on devices that aren't NTP synced.
|
|
||||||
}());
|
}());
|
@ -12,7 +12,7 @@
|
|||||||
"debug": "node --prof-process --preprocess -j isolate*.log > v8data.json && rm isolate*.log && echo 'drag & drop ./v8data.json into https://mapbox.github.io/flamebearer/'",
|
"debug": "node --prof-process --preprocess -j isolate*.log > v8data.json && rm isolate*.log && echo 'drag & drop ./v8data.json into https://mapbox.github.io/flamebearer/'",
|
||||||
"https": "HTTPS_KEY=test/https/server.key HTTPS_CERT=test/https/server.crt npm start",
|
"https": "HTTPS_KEY=test/https/server.key HTTPS_CERT=test/https/server.crt npm start",
|
||||||
"prepublishOnly": "npm run unbuild",
|
"prepublishOnly": "npm run unbuild",
|
||||||
"test": "mocha && echo 'Did you run PANIC holy-grail, 1~X, on-recover, etc.?'",
|
"test": "echo 'Did you run PANIC holy-grail, 1~X, on-recover, etc.?' && mocha",
|
||||||
"testsea": "mocha test/sea/sea.js",
|
"testsea": "mocha test/sea/sea.js",
|
||||||
"testaxe": "mocha test/axe/holy-grail.js",
|
"testaxe": "mocha test/axe/holy-grail.js",
|
||||||
"e2e": "mocha e2e/distributed.js",
|
"e2e": "mocha e2e/distributed.js",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user