-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
645d610
commit b93c978
Showing
2 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
(() => { | ||
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}`; | ||
|
||
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; | ||
reconnectModal.style.display = 'none'; | ||
} | ||
}; | ||
}; | ||
|
||
let currentReconnectionProcess = null; | ||
|
||
Blazor.start({ | ||
reconnectionHandler: { | ||
onConnectionDown: () => currentReconnectionProcess ??= startReconnectionProcess(), | ||
onConnectionUp: () => { | ||
currentReconnectionProcess?.cancel(); | ||
currentReconnectionProcess = null; | ||
} | ||
} | ||
}); | ||
})(); |