UNPKG

timesync

Version:

Time synchronization between peers

54 lines (47 loc) 1.45 kB
<!DOCTYPE html> <html> <head> <!-- include es5-shim and es5-shim when support for older browsers is needed --> <script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.0.5/es5-shim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.23.0/es6-shim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="/timesync/timesync.js"></script> <script> var ts = timesync.create({ peers: ['/timesync'], interval: 10000 }); ts.on('sync', function (state) { console.log('sync ' + state); }); ts.on('change', function (offset) { console.log('changed offset: ' + offset + ' ms'); }); ts.send = function (to, data, timeout) { //console.log('send', data); return new Promise(function (resolve, reject) { $.ajax({ url: to, type: 'POST', data: JSON.stringify(data), contentType : 'application/json', dataType: 'json', // response type timeout: timeout }) .done(function (data) { //console.log('receive', data); ts.receive(to, data); resolve(); }) .fail(function (err) { console.log('Error', err); reject(err); }); }); }; </script> </head> <body> (see outputs in the developer console) </body> </html>