67 lines
2.1 KiB
JavaScript
67 lines
2.1 KiB
JavaScript
(() => {
|
|
const maximumRetryCount = 1800;
|
|
const retryIntervalMilliseconds = 2000;
|
|
const rectModalBox = document.getElementById('reconnect-modal');
|
|
const recModal = document.getElementById('reconnect-modal-text');
|
|
|
|
const startReconnectionProcess = () => {
|
|
rectModalBox.style.display = 'block';
|
|
|
|
let isCanceled = false;
|
|
|
|
(async () => {
|
|
for (let i = 0; i < maximumRetryCount; i++) {
|
|
recModal.innerText = `Tentativo riconnessione ${i + 1}...`;
|
|
//recModal.innerText = `Tentativo riconnessione: ${i + 1} / ${maximumRetryCount}`;
|
|
|
|
await new Promise(resolve => setTimeout(resolve, retryIntervalMilliseconds));
|
|
|
|
if (isCanceled) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const result = await Blazor.reconnect();
|
|
if (!result) {
|
|
// The server was reached, but the connection was rejected; reload the page.
|
|
location.reload();
|
|
return;
|
|
}
|
|
|
|
// Successfully reconnected to the server.
|
|
return;
|
|
} catch {
|
|
// Didn't reach the server; try again.
|
|
}
|
|
}
|
|
|
|
// Retried too many times; reload the page.
|
|
location.reload();
|
|
})();
|
|
|
|
return {
|
|
cancel: () => {
|
|
isCanceled = true;
|
|
rectModalBox.style.display = 'none';
|
|
},
|
|
};
|
|
};
|
|
|
|
let currentReconnectionProcess = null;
|
|
|
|
Blazor.start({
|
|
circuit: {
|
|
reconnectionHandler: {
|
|
onConnectionDown: () => {
|
|
currentReconnectionProcess ??= startReconnectionProcess();
|
|
console.log("Connection Lost!");
|
|
},
|
|
onConnectionUp: () => {
|
|
currentReconnectionProcess?.cancel();
|
|
currentReconnectionProcess = null;
|
|
console.log("Connection restored!");
|
|
}
|
|
}
|
|
}
|
|
});
|
|
})(); |