Forked from SQL-MisterMagoo/Custom Blazor Startup.html
Created
April 11, 2020 18:50
-
-
Save galvesribeiro/7485403049b6c93859bd67cd811d54c2 to your computer and use it in GitHub Desktop.
Custom Blazor Startup Example with custom Retries/Interval and custom Reconnection Handler (not production code)
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
<script autostart="false" src="_framework/blazor.server.js"></script> | |
<script> | |
async function connectionDown(options) { | |
console.log("Connection Down - you could do some UI here..."); | |
for (let i = 0; i < options.maxRetries; i++) { | |
console.log("Waiting for reconnect attempt #"+(i+1)+" ..."); | |
await this.delay(options.retryIntervalMilliseconds); | |
if (this.isDisposed) { | |
break; | |
} | |
try { | |
// reconnectCallback will asynchronously return: | |
// - true to mean success | |
// - false to mean we reached the server, but it rejected the connection (e.g., unknown circuit ID) | |
// - exception to mean we didn't reach the server (this can be sync or async) | |
console.log("Starting Reconnect attempt #"+(i+1)+" ..."); | |
const result = await window.Blazor.reconnect(); | |
if (!result) { | |
// If the server responded and refused to reconnect, log it | |
console.error("Server Rejected"); | |
} else { | |
// Reconnected! | |
return; | |
} | |
} catch (err) { | |
// We got an exception so will try again | |
console.error(err); | |
} | |
} | |
// all attempts failed - let's try a full reload | |
// This could be a UI change instead or something more complicated | |
location.reload(); | |
} | |
function delay(durationMilliseconds) { | |
return new Promise(resolve => setTimeout(resolve, durationMilliseconds)); | |
} | |
function connectionUp(e) { | |
// Reconnected | |
console.log("Connection UP!"); | |
// if you have a UI to hide/change you can do that here. | |
} | |
window.Blazor.start({ | |
reconnectionOptions: { | |
maxRetries: 30, | |
retryIntervalMilliseconds: 500, | |
}, | |
reconnectionHandler: { | |
onConnectionDown: e => connectionDown(e), | |
onConnectionUp: e => connectionUp(e) | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment