startrally added autoreconnect if websocket is error

This commit is contained in:
Bluedragon 2022-05-02 03:19:50 +07:00
parent d65d980642
commit 7e2ed058a6
3 changed files with 203 additions and 121 deletions

View File

@ -10,13 +10,38 @@
<script type = "text/javascript">
var ping;
var ws;
var wsInterval;
window.onerror = function (message, file, line, col, error) {
const date = new Date();
console.log(date.toLocaleTimeString()+";Onerror->Error occurred: " + error.message);
return false;
};
function checkWS(){
const date = new Date();
console.log(date.toLocaleTimeString()+";checkWS")
if (!ws || ws.readyState === 3 ) loaded();
}
function wsStartTimer(){
const date = new Date();
console.log(date.toLocaleTimeString()+";startwsStartTimer")
wsInterval = setInterval(checkWS(), 1000);
}
function wsStopTimer(){
const date = new Date();
console.log(date.toLocaleTimeString()+";clearWSInterval")
if (wsInterval) clearInterval(wsInterval);
console.log(wsInterval);
}
function loaded(){
ws = new WebSocket("ws://%serveripaddress%/ws");
console.log("new ws");
const date = new Date();
console.log(date.toLocaleTimeString()+";new ws");
// const ws = new WebSocket("ws://%serveripaddress%/ws");
ws.onopen = function() {
console.log("WebSocket Connected");
const date = new Date();
console.log(date.toLocaleTimeString()+";WebSocket Connected");
wsStopTimer();
if(document.getElementById("mainTime").classList.contains("bg-danger")){
document.getElementById("mainTime").classList.remove("bg-danger")
}
@ -24,11 +49,18 @@
};
ws.onclose = function() {
// alert("WS Connection Closed");
// console.log("WS Connection Closed");
const date = new Date();
console.log(date.toLocaleTimeString()+";WS Connection Closed");
if(!document.getElementById("mainTime").classList.contains("bg-danger")){
document.getElementById("mainTime").classList.add("bg-danger")
}
ping = -1;
wsStartTimer();
};
ws.onerror = function(err) {
const date = new Date();
console.error(date.toLocaleTimeString()+';Socket encountered error: ', err.message, 'Closing socket');
ws.close();
};
ws.onmessage = function(event) {
// console.log(event.data);
@ -68,9 +100,11 @@
// }
};
}
setInterval(checkPing, 1000); //heartbeat
const checkhealt = setInterval(checkPing, 1000); //heartbeat
function checkPing(){
// console.log("ping="+ping);
// const date = new Date();
// console.log(date.toLocaleTimeString()+";ping="+ping);
ping--;
if (ping>=0){
ping=0;
@ -80,8 +114,10 @@
if(!document.getElementById("mainTime").classList.contains("bg-danger")){
document.getElementById("mainTime").classList.add("bg-danger")
}
}
}
function onlyNumberKey(evt,e) {
// Only ASCII character in that range allowed
var ASCIICode = (evt.which) ? evt.which : evt.keyCode

View File

@ -11,13 +11,38 @@ const char HTML_startrally[] PROGMEM = R"rawliteral(
<script type = "text/javascript">
var ping;
var ws;
var wsInterval;
window.onerror = function (message, file, line, col, error) {
const date = new Date();
console.log(date.toLocaleTimeString()+";Onerror->Error occurred: " + error.message);
return false;
};
function checkWS(){
const date = new Date();
console.log(date.toLocaleTimeString()+";checkWS")
if (!ws || ws.readyState === 3 ) loaded();
}
function wsStartTimer(){
const date = new Date();
console.log(date.toLocaleTimeString()+";startwsStartTimer")
wsInterval = setInterval(checkWS(), 1000);
}
function wsStopTimer(){
const date = new Date();
console.log(date.toLocaleTimeString()+";clearWSInterval")
if (wsInterval) clearInterval(wsInterval);
console.log(wsInterval);
}
function loaded(){
ws = new WebSocket("ws://%serveripaddress%/ws");
console.log("new ws");
const date = new Date();
console.log(date.toLocaleTimeString()+";new ws");
// const ws = new WebSocket("ws://%serveripaddress%/ws");
ws.onopen = function() {
console.log("WebSocket Connected");
const date = new Date();
console.log(date.toLocaleTimeString()+";WebSocket Connected");
wsStopTimer();
if(document.getElementById("mainTime").classList.contains("bg-danger")){
document.getElementById("mainTime").classList.remove("bg-danger")
}
@ -25,11 +50,18 @@ const char HTML_startrally[] PROGMEM = R"rawliteral(
};
ws.onclose = function() {
// alert("WS Connection Closed");
// console.log("WS Connection Closed");
const date = new Date();
console.log(date.toLocaleTimeString()+";WS Connection Closed");
if(!document.getElementById("mainTime").classList.contains("bg-danger")){
document.getElementById("mainTime").classList.add("bg-danger")
}
ping = -1;
wsStartTimer();
};
ws.onerror = function(err) {
const date = new Date();
console.error(date.toLocaleTimeString()+';Socket encountered error: ', err.message, 'Closing socket');
ws.close();
};
ws.onmessage = function(event) {
// console.log(event.data);
@ -69,9 +101,11 @@ const char HTML_startrally[] PROGMEM = R"rawliteral(
// }
};
}
setInterval(checkPing, 1000); //heartbeat
const checkhealt = setInterval(checkPing, 1000); //heartbeat
function checkPing(){
// console.log("ping="+ping);
// const date = new Date();
// console.log(date.toLocaleTimeString()+";ping="+ping);
ping--;
if (ping>=0){
ping=0;
@ -81,8 +115,10 @@ const char HTML_startrally[] PROGMEM = R"rawliteral(
if(!document.getElementById("mainTime").classList.contains("bg-danger")){
document.getElementById("mainTime").classList.add("bg-danger")
}
}
}
function onlyNumberKey(evt,e) {
// Only ASCII character in that range allowed
var ASCIICode = (evt.which) ? evt.which : evt.keyCode

View File

@ -4321,23 +4321,33 @@ void loop() //loop from VSS RALLY V2
// Serial.print("calculationRallyTime=");Serial.println(micros()-broadcastwebsocket);
iscalculatingRally=false;
// Serial.println(strRallyJson);
if (strRallyJson){
ws.textAll(strRallyJson);
} else {
Serial.print("strRallyJson error:");Serial.println(strRallyJson);
}
// Serial.print("calculationRallyToTTime=");Serial.println(micros()-broadcastwebsocket);
broadcastwebsocket = millis();
}
if ((millis()-timerWebsocketCalibration) > 499 && !isloadingConst && gHasCalibClient) {
timerWebsocketCalibration = millis();
// if (gHasCalibClient) {
redrawcalibrationMenuForESP32();
if (strCalibJson){
wscal.textAll(strCalibJson);
// }
} else {
Serial.print("strCalibJson error:");Serial.println(strCalibJson);
}
}
if ((millis()-timerWebsocketSetTime) > 450 && gHasSetTimeClient) {
timerWebsocketSetTime = millis();
fillSetTimeJsonForESP32();
if (strSetTimeJson){
wssettime.textAll(strSetTimeJson);
} else {
Serial.print("strSetTimeJson error:");Serial.println(strSetTimeJson);
}
}
if ((millis()-timerDS3231Get) > 500 && (gHasCalibClient || gHasRallyClient || gHasSetTimeClient || gHasLcdClient)) {