Don't try to reconnect if the network is down

This commit is contained in:
Simon Ser 2021-06-11 09:36:11 +02:00
parent 56c18e3810
commit 02ba4be438

View File

@ -69,6 +69,7 @@ export default class Client extends EventTarget {
this.disconnect(); this.disconnect();
this.autoReconnect = autoReconnect; this.autoReconnect = autoReconnect;
console.log("Connecting to " + this.params.url);
this.setStatus(Client.Status.CONNECTING); this.setStatus(Client.Status.CONNECTING);
try { try {
@ -98,12 +99,21 @@ export default class Client extends EventTarget {
this.isupport = new Map(); this.isupport = new Map();
if (this.autoReconnect) { if (this.autoReconnect) {
if (!navigator.onLine) {
console.info("Waiting for network to go back online");
const handleOnline = () => {
window.removeEventListener("online", handleOnline);
this.reconnect();
};
window.addEventListener("online", handleOnline);
} else {
console.info("Reconnecting to server in " + RECONNECT_DELAY_SEC + " seconds"); console.info("Reconnecting to server in " + RECONNECT_DELAY_SEC + " seconds");
clearTimeout(this.reconnectTimeoutID); clearTimeout(this.reconnectTimeoutID);
this.reconnectTimeoutID = setTimeout(() => { this.reconnectTimeoutID = setTimeout(() => {
this.reconnect(); this.reconnect();
}, RECONNECT_DELAY_SEC * 1000); }, RECONNECT_DELAY_SEC * 1000);
} }
}
}); });
this.ws.addEventListener("error", () => { this.ws.addEventListener("error", () => {