Skip to content

Commit

Permalink
Merge pull request #363 from NielsPilgaard/bugfix/blazor-reconnection
Browse files Browse the repository at this point in the history
A lot more retries, don't show the user the connection is down
  • Loading branch information
NielsPilgaard authored May 5, 2024
2 parents 642c7b6 + c687b75 commit e0e132c
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions src/web/Jordnaer/wwwroot/js/boot.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
(() => {
const maximumRetryCount = 3;
const retryIntervalMilliseconds = 5000;
const reconnectModal = document.getElementById('reconnect-modal');

const startReconnectionProcess = () => {
reconnectModal.style.display = 'block';

let isCanceled = false;

(async () => {
for (let i = 0; i < maximumRetryCount; i++) {
reconnectModal.innerText = `Attempting to reconnect: ${i + 1} of ${maximumRetryCount}`;

while(true) {
await new Promise(resolve => setTimeout(resolve, retryIntervalMilliseconds));

if (isCanceled) {
Expand All @@ -28,31 +22,28 @@

// Successfully reconnected to the server.
return;
} catch {
// Didn't reach the server; try again.
} catch (exception) {
}
}

// Retried too many times; reload the page.
location.reload();
})();

return {
cancel: () => {
isCanceled = true;
reconnectModal.style.display = 'none';
}
};
};

let currentReconnectionProcess = null;

Blazor.start({
reconnectionHandler: {
onConnectionDown: () => currentReconnectionProcess ??= startReconnectionProcess(),
onConnectionUp: () => {
currentReconnectionProcess?.cancel();
currentReconnectionProcess = null;
circuit: {
reconnectionHandler: {
onConnectionDown: () => currentReconnectionProcess ??= startReconnectionProcess(),
onConnectionUp: () => {
currentReconnectionProcess?.cancel();
currentReconnectionProcess = null;
}
}
}
});
Expand Down

0 comments on commit e0e132c

Please sign in to comment.