From ecfa61ab231482d6403add3f163250564824fc62 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Tue, 8 Oct 2013 19:14:13 +0000 Subject: svg/js: enable longpolling --- svg/subcan.js | 46 ++++++++++++++++++++++++++++++++++++++++++---- svg/subcan_dxf.svg | 16 ++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/svg/subcan.js b/svg/subcan.js index 6c56062..63d0e74 100644 --- a/svg/subcan.js +++ b/svg/subcan.js @@ -394,14 +394,27 @@ function update_elements(json) { } var xhr = new XMLHttpRequest(); +var xhrtimeout = null; +var xhrlastref = null; + function xhr_handler() { if (xhr.readyState === 4) { if (xhr.status === 200) { - data = JSON.parse(xhr.responseText); + if (xhr.responseText != '') { + rdata = JSON.parse(xhr.responseText); + data = rdata['data']; + xhrlastref = rdata['ref']; + } + console.log('xhr OK', xhrlastref); + + timer(0); + /* for (attr in data) { console.log('xhr attr', attr); } */ update_elements(data); + } else if (xhr.status === 0) { + /* request abort, ignore */ } else { console.log('xhr: something went wrong', xhr.status, xhr.statusText); } @@ -409,11 +422,36 @@ function xhr_handler() { } xhr.onreadystatechange = xhr_handler; -function timer() { - xhr.open('GET', '/'); +function ISOTS() { + function pad(n){return n<10 ? '0'+n : n} + var d = new Date(); + return d.getUTCFullYear()+'-' + + pad(d.getUTCMonth()+1)+'-' + + pad(d.getUTCDate())+'T' + + pad(d.getUTCHours())+':' + + pad(d.getUTCMinutes())+':' + + pad(d.getUTCSeconds())+'Z'; +} + +function timer(expired) { + var dbgtext = document.getElementById('dbgtextreal'); + + if (expired) { + xhr.abort(); + } else { + window.clearTimeout(xhrtimeout); + } + + if (xhrlastref === null) { + xhr.open('GET', '/longpoll'); + dbgtext.textContent = ISOTS() + ' (none)'; + } else { + xhr.open('GET', '/longpoll?' + xhrlastref); + dbgtext.textContent = ISOTS() + ' ' + xhrlastref; + } // xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); xhr.send(null); - window.setTimeout(timer, 2000); + xhrtimeout = window.setTimeout(timer, 45000, 1); } function doload() { diff --git a/svg/subcan_dxf.svg b/svg/subcan_dxf.svg index 456ccb9..7e923c2 100644 --- a/svg/subcan_dxf.svg +++ b/svg/subcan_dxf.svg @@ -1300,6 +1300,22 @@ inkscape:connector-curvature="0" sodipodi:nodetypes="cc" /> + + loading... +