Skip to content

Commit

Permalink
WS pause when document not visible
Browse files Browse the repository at this point in the history
  • Loading branch information
forkineye committed Sep 28, 2021
1 parent ddd6bd9 commit f5bb0a2
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions html/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var wsOutputQueue = [];
var wsBusy = false;
var wsPaused = false;
var wsOutputQueueTimer = null;
var StatusRequestTimer = null;
var FseqFileListRequestTimer = null;
Expand All @@ -16,6 +17,8 @@ var selector = [];
var target = null;
var SdCardIsInstalled = false;
var FseqFileTransferStartTime = new Date();
var pingTimer;
var pongTimer;

// Drawing canvas - move to diagnostics
var canvas = document.getElementById("canvas");
Expand Down Expand Up @@ -251,6 +254,17 @@ $(function ()
let hash = window.location.hash;
hash && $('ul.navbar-nav li a[href="' + hash + '"]').click();

// Halt pingpong if document is not visible
document.addEventListener("visibilitychange", function() {
if (document.hidden) {
clearTimeout(pingTimer);
clearTimeout(pongTimer);
} else {
wsReadyToSend();
wsPingPong();
}
});

// triggers menu update
RequestListOfFiles();
});
Expand Down Expand Up @@ -1016,8 +1030,6 @@ function int2ip(num)
//
////////////////////////////////////////////////////
// On websocket connect
var pingTimer;
var pongTimer;
function wsConnect()
{
if ('WebSocket' in window)
Expand Down Expand Up @@ -1192,11 +1204,17 @@ function wsReconnect()
// Websocket message queuer
function wsEnqueue(message)
{
// only send messages if the WS interface is up
// only send messages if the WS interface is up and document is visible
if (ws.readyState !== 1)
{
// console.info("WS is down. Discarding msg: " + message);
console.debug ("WS is down - Discarding msg: " + message);
}

else if (wsPaused)
{
console.debug ("WS Paused - Discarding msg: " + message)
}

else
{
wsOutputQueue.push(message);
Expand Down Expand Up @@ -1240,6 +1258,20 @@ function wsProcessOutputQueue()
wsFlushAndHaltTheOutputQueue();
}

// Pause processing
else if (document.hidden)
{
console.debug (`WS Paused - Holding msg: ${wsOutputQueue}`);
wsPaused = true;
if (null !== wsOutputQueueTimer)
{
// stop the timer
clearTimeout(wsOutputQueueTimer);
wsOutputQueueTimer = null;
wsBusy = true;
}
}

//check if we are currently waiting for a response
else if (wsBusy === true)
{
Expand Down Expand Up @@ -1297,6 +1329,7 @@ function wsReadyToSend()

// show we are ready to send the next message
wsBusy = false;
wsPaused = false;

//send next message
wsProcessOutputQueue();
Expand Down Expand Up @@ -1386,8 +1419,6 @@ function ProcessReceivedJsonAdminMessage(data)
// ProcessReceivedJsonStatusMessage
function ProcessReceivedJsonStatusMessage(data)
{
console.debug("Status: " + data);

let JsonStat = JSON.parse(data);
let Status = JsonStat.status;
let System = Status.system;
Expand Down

0 comments on commit f5bb0a2

Please sign in to comment.