Skip to content

Commit cfff287

Browse files
Close and re-open websocket on 'visiblityState' change.
Force close websocket on page close.
1 parent 7de373a commit cfff287

File tree

6 files changed

+822
-784
lines changed

6 files changed

+822
-784
lines changed

dagelijks.htm

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en">
33
<head>
44
<script>
5-
const debugip="192.168.0.106"; //change this to the esp32 ip for remote running of this page
5+
const debugip="192.168.0.90"; //change this to the esp32 ip for remote running of this page
66
</script>
77
<meta charset="utf-8">
88
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
@@ -488,12 +488,10 @@
488488

489489
ws.onclose = function(event){
490490
console.log('[WebSocket] Connection died, code=' + event.code + ' reason=' + event.reason);
491-
ws.open();
492491
}
493492

494493
ws.onerror = function(error){
495494
console.log('[WebSocket] ' + ws_host + ' Error: ' + error.message);
496-
ws.refresh();
497495
}
498496

499497
document.getElementById('prevbutton').addEventListener('mouseup', function() {
@@ -584,10 +582,22 @@
584582
currentDay = new Date();
585583
document.getElementById('startRange').focus();
586584

587-
document.addEventListener('visibilitychange', e =>{
588-
if (document.visibilityState === 'visible' && currentDayEqualsToday())
589-
getData(new Date(currentDay), drawGraph, false);
585+
document.addEventListener('visibilitychange', e=>{
586+
switch(document.visibilityState) {
587+
case 'visible' : {
588+
ws.open();
589+
if (currentDayEqualsToday())
590+
getData(new Date(currentDay), drawGraph, false);
591+
}
592+
break;
593+
case 'hidden' : {
594+
ws.close();
595+
}
596+
default : {}
597+
}
590598
});
599+
600+
document.addEventListener("unload", e=>{ws.close();});
591601
})
592602

593603
function drawBackground() {

dagelijks.htm.gz

32 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)