add proper WS connection retrying
This commit is contained in:
parent
8fe0c9f46b
commit
085bc4bc79
2 changed files with 42 additions and 17 deletions
|
@ -12,7 +12,13 @@
|
||||||
<body class="Dark-theme">
|
<body class="Dark-theme">
|
||||||
<script src="/index.js" type="module"></script>
|
<script src="/index.js" type="module"></script>
|
||||||
|
|
||||||
<div id="loading" class="loading"><div id="centerdiv"><img src="/bitmap.svg" width="1in" height="1in"><h1>Jank Client is loading</h1><h2>This shouldn't take long</h2></div></div>
|
<div id="loading" class="loading">
|
||||||
|
<div id="centerdiv">
|
||||||
|
<img src="/bitmap.svg" width="1in" height="1in">
|
||||||
|
<h1>Jank Client is loading</h1>
|
||||||
|
<h2 id="load-desc">This shouldn't take long</h2>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="flexltr" id="page">
|
<div class="flexltr" id="page">
|
||||||
<div id="neunence">
|
<div id="neunence">
|
||||||
<div id="servers"></div>
|
<div id="servers"></div>
|
||||||
|
|
|
@ -7,6 +7,9 @@ import {Member} from "./member.js";
|
||||||
import {markdown} from "./markdown.js";
|
import {markdown} from "./markdown.js";
|
||||||
import {Fullscreen} from "./fullscreen.js";
|
import {Fullscreen} from "./fullscreen.js";
|
||||||
import {setTheme, Specialuser} from "./login.js";
|
import {setTheme, Specialuser} from "./login.js";
|
||||||
|
|
||||||
|
const wsCodesRetry=new Set([4000,4003,4005,4007,4008,4009]);
|
||||||
|
|
||||||
class Localuser{
|
class Localuser{
|
||||||
packets:number;
|
packets:number;
|
||||||
token:string;
|
token:string;
|
||||||
|
@ -27,6 +30,8 @@ class Localuser{
|
||||||
ws:WebSocket;
|
ws:WebSocket;
|
||||||
typing:[string,number][];
|
typing:[string,number][];
|
||||||
wsinterval:NodeJS.Timeout;
|
wsinterval:NodeJS.Timeout;
|
||||||
|
connectionSucceed=0;
|
||||||
|
errorBackoff=0;
|
||||||
constructor(userinfo:Specialuser){
|
constructor(userinfo:Specialuser){
|
||||||
this.packets=1;
|
this.packets=1;
|
||||||
this.token=userinfo.token;
|
this.token=userinfo.token;
|
||||||
|
@ -215,6 +220,8 @@ class Localuser{
|
||||||
}else if(temp.op===10){
|
}else if(temp.op===10){
|
||||||
console.log("heartbeat down")
|
console.log("heartbeat down")
|
||||||
this.wsinterval=setInterval(_=>{
|
this.wsinterval=setInterval(_=>{
|
||||||
|
if (this.connectionSucceed===0) this.connectionSucceed=Date.now()
|
||||||
|
|
||||||
this.ws.send(JSON.stringify({op:1,d:this.packets}))
|
this.ws.send(JSON.stringify({op:1,d:this.packets}))
|
||||||
},temp.d.heartbeat_interval)
|
},temp.d.heartbeat_interval)
|
||||||
this.packets=1;
|
this.packets=1;
|
||||||
|
@ -227,23 +234,35 @@ class Localuser{
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.ws.addEventListener('close', (event) => {
|
this.ws.addEventListener("close", event => {
|
||||||
clearInterval(this.wsinterval);
|
console.log("WebSocket closed with code " + event.code);
|
||||||
console.log('WebSocket closed');
|
if (this.wsinterval) clearInterval(this.wsinterval);
|
||||||
console.warn(event);
|
|
||||||
if(event.code!==4000){
|
this.unload();
|
||||||
this.unload();
|
document.getElementById("loading").classList.remove("doneloading");
|
||||||
document.getElementById("loading").classList.remove("doneloading");
|
document.getElementById("loading").classList.add("loading");
|
||||||
document.getElementById("loading").classList.add("loading");
|
|
||||||
this.initwebsocket().then(_=>{
|
if (((event.code>1000 && event.code<1016) || wsCodesRetry.has(event.code))) {
|
||||||
this.loaduser();
|
if (this.connectionSucceed!==0 && Date.now()>this.connectionSucceed+20000) this.errorBackoff=0;
|
||||||
this.init();
|
else this.errorBackoff++;
|
||||||
document.getElementById("loading").classList.add("doneloading");
|
this.connectionSucceed=0;
|
||||||
document.getElementById("loading").classList.remove("loading");
|
|
||||||
console.log("done loading")
|
document.getElementById("load-desc").innerHTML="Unable to connect to the Spacebar server, retrying in <b>" + Math.round(0.2 + (this.errorBackoff*2.8)) + "</b> seconds...";
|
||||||
});
|
|
||||||
}
|
setTimeout(() => {
|
||||||
|
document.getElementById("load-desc").textContent="Retrying...";
|
||||||
|
|
||||||
|
this.initwebsocket().then(() => {
|
||||||
|
this.loaduser();
|
||||||
|
this.init();
|
||||||
|
document.getElementById("loading").classList.add("doneloading");
|
||||||
|
document.getElementById("loading").classList.remove("loading");
|
||||||
|
console.log("done loading");
|
||||||
|
});
|
||||||
|
}, 200 + (this.errorBackoff*2800));
|
||||||
|
} else document.getElementById("load-desc").textContent="Unable to connect to the Spacebar server. Please try logging out and back in.";
|
||||||
});
|
});
|
||||||
|
|
||||||
await promise;
|
await promise;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue