Skip to content

Commit 20a9448

Browse files
Mark JessopMark Jessop
authored andcommitted
Remove datetime checks on websockets data. Use page lifecycle resume event to trigger polled refresh.
1 parent 6f5e78e commit 20a9448

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

js/tracker.js

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3535,7 +3535,7 @@ function refresh() {
35353535
document.getElementById("timeperiod").disabled = false;
35363536
}
35373537
clientActive = true;
3538-
console.log("WebSockets - Resuming Websockets updates.")
3538+
//console.log("WebSockets - Resuming Websockets updates after poll.")
35393539
clearTimeout(periodical);
35403540
ajax_inprogress = false;
35413541
}
@@ -3631,47 +3631,60 @@ function liveData() {
36313631
var frame = JSON.parse(message.payloadString.toString());
36323632

36333633
if (wvar.query == "" || sondePrefix.indexOf(wvar.query) > -1 || wvar.query == frame.serial) {
3634-
if (frame.length == null) {
3635-
var tempDate = new Date(frame.datetime).getTime();
3636-
} else {
3637-
var tempDate = new Date(frame[frame.length - 1].datetime).getTime()
3638-
}
3639-
if ((dateNow - tempDate) < 100000) {
3640-
// Data is recent enough.
3641-
var test = formatData(frame, true);
3642-
if (clientActive) {
3643-
live_data_buffer.positions.position.push.apply(live_data_buffer.positions.position,test.positions.position)
3644-
}
3634+
3635+
var test = formatData(frame, true);
3636+
if (clientActive) {
3637+
live_data_buffer.positions.position.push.apply(live_data_buffer.positions.position,test.positions.position)
36453638
$("#stTimer").attr("data-timestamp", dateNow);
3646-
$("#stText").text("websocket ("+ ((dateNow - tempDate)/1000).toFixed(1) +" s) |");
3647-
} else if ((dateNow - new Date(frame.datetime).getTime()) > 150000) {
3648-
// Data is very old, which may indicate that our tab has probably been suspended.
3649-
// We now poll for data.
3650-
$("#stText").text("data error ("+ ((dateNow - tempDate)/1000).toFixed(1) +" s) |");
3651-
console.log("WebSockets - Data Age was " + ((dateNow - tempDate)/1000).toFixed(1) + " s, frame length: " + frame.length + ". Discarding and polling for data.");
3652-
// Discard all further messages until we have finished the next poll.
3653-
console.log("WebSockets - Tab possibly suspended, polling for updates.")
3654-
clientActive = false;
3655-
refresh();
3656-
} else {
3657-
$("#stText").text("data error ("+ ((dateNow - tempDate)/1000).toFixed(1) +" s) |");
3658-
console.log("WebSockets - Data Age was " + ((dateNow - tempDate)/1000).toFixed(1) + " s, frame length: " + frame.length + ". Discarding frame.");
3639+
$("#stText").text("websocket |");
36593640
}
3641+
3642+
// if (frame.length == null) {
3643+
// var tempDate = new Date(frame.datetime).getTime();
3644+
// } else {
3645+
// var tempDate = new Date(frame[frame.length - 1].datetime).getTime()
3646+
// }
3647+
// if ((dateNow - tempDate) < 100000) {
3648+
// // Data is recent enough.
3649+
// var test = formatData(frame, true);
3650+
// if (clientActive) {
3651+
// live_data_buffer.positions.position.push.apply(live_data_buffer.positions.position,test.positions.position)
3652+
// }
3653+
// $("#stTimer").attr("data-timestamp", dateNow);
3654+
// $("#stText").text("websocket ("+ ((dateNow - tempDate)/1000).toFixed(1) +" s) |");
3655+
// } else if ((dateNow - new Date(frame.datetime).getTime()) > 150000) {
3656+
// // Data is very old, which may indicate that our tab has probably been suspended.
3657+
// // We now poll for data.
3658+
// $("#stText").text("data error ("+ ((dateNow - tempDate)/1000).toFixed(1) +" s) |");
3659+
// console.log("WebSockets - Data Age was " + ((dateNow - tempDate)/1000).toFixed(1) + " s, frame length: " + frame.length + ". Discarding and polling for data.");
3660+
// // Discard all further messages until we have finished the next poll.
3661+
// console.log("WebSockets - Tab possibly suspended, polling for updates.")
3662+
// clientActive = false;
3663+
// refresh();
3664+
// } else {
3665+
// $("#stText").text("data error ("+ ((dateNow - tempDate)/1000).toFixed(1) +" s) |");
3666+
// console.log("WebSockets - Data Age was " + ((dateNow - tempDate)/1000).toFixed(1) + " s, frame length: " + frame.length + ". Discarding frame.");
3667+
// }
36603668
}
36613669
}
36623670
} else {
3663-
console.log("WebSockets - Discarding Message.")
3671+
console.log("WebSockets - Discarding Message, not ready yet.")
36643672
}
36653673
}
36663674
catch(err) {}
36673675
};
36683676
}
36693677

3678+
3679+
// Interval to read in the live data buffer and update the page.
36703680
setInterval(function(){
36713681
update(live_data_buffer);
36723682
live_data_buffer.positions.position=[];
36733683
}, 500)
36743684

3685+
// Event listener to update on page resume from suspend.
3686+
document.addEventListener('resume', refresh);
3687+
36753688
function refreshSingle(serial) {
36763689
if(ajax_inprogress_single) {
36773690
clearTimeout(periodical_focus);

0 commit comments

Comments
 (0)