Retry connections or reconnect failuers

This commit is contained in:
David Botton 2024-02-11 21:04:31 -05:00
parent affadab80a
commit e19ac8bc42

View file

@ -2,6 +2,7 @@ var ws=null;
var adr; var adrc; var adr; var adrc;
var clog={}; var clog={};
var pingerid; var pingerid;
var retryid;
var s = document.location.search; var s = document.location.search;
var tokens; var tokens;
var r = /[?&]?([^=]+)=([^&]*)/g; var r = /[?&]?([^=]+)=([^&]*)/g;
@ -32,12 +33,15 @@ function Shutdown_ws(event) {
ws = null; ws = null;
} }
clearInterval (pingerid); clearInterval (pingerid);
clearInterval (retryid);
if (clog['html_on_close'] != '') { if (clog['html_on_close'] != '') {
$(document.body).html(clog['html_on_close']); $(document.body).html(clog['html_on_close']);
} }
} }
function Setup_ws() { function Setup_ws() {
clearInterval (retryid);
retryid = 0;
ws.onmessage = function (event) { ws.onmessage = function (event) {
try { try {
if (clog_debug == true) { if (clog_debug == true) {
@ -49,36 +53,33 @@ function Setup_ws() {
} }
} }
ws.onerror = function (event) { var rc = function (event) {
console.log ('onerror: reconnect'); console.log (event);
ws = null; ws = null;
ws = new WebSocket (adr + '?r=' + clog['connection_id']); ws = new WebSocket (adr + '?r=' + clog['connection_id']);
ws.onopen = function (event) { ws.onopen = function (event) {
console.log ('onerror: reconnect successful'); console.log ('reconnect successful');
Setup_ws(); Setup_ws();
} }
ws.onclose = function (event) { ws.onclose = function (event) {
console.log ('onerror: reconnect failure'); console.log ('reconnect failure');
Shutdown_ws(event); console.log (Date.now());
if (retryid == 0)
retryid = setInterval(function () {rc("Failed reconnect - trying again")}, 500);
} }
} }
ws.onerror = function (event) {
console.log ('onerror: reconnect');
rc("onerror - trying reconnect")
}
ws.onclose = function (event) { ws.onclose = function (event) {
if (event.code && event.code === 1000) { if (event.code && event.code === 1000) {
console.log("WebSocket connection got normal close from server. Don't reconnect."); console.log("WebSocket connection got normal close from server. Don't reconnect.");
Shutdown_ws(event); Shutdown_ws(event);
} else { } else {
console.log ('onclose: reconnect'); rc("onclose - trying reconnnect");
ws = null;
ws = new WebSocket (adr + '?r=' + clog['connection_id']);
ws.onopen = function (event) {
console.log ('onclose: reconnect successful');
Setup_ws();
}
ws.onclose = function (event) {
console.log ('onclose: reconnect failure');
Shutdown_ws(event);
}
} }
} }
} }