Advertisement
Guest User

blazor server boot.js

a guest
Apr 9th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.17 KB | Source Code | 0 0
  1. window.blazorIsConnected = function () {
  2.     // Check if Blazor exists and has internal components initialized
  3.     if (!Blazor || !Blazor._internal || !Blazor._internal.navigationManager) {
  4.         return false; // Explicitly return false instead of undefined/null
  5.     }
  6.  
  7.     // Return true only if explicitly connected
  8.     return Blazor._internal.navigationManager._isConnected === true;
  9. };
  10.  
  11. window.registerConnectionRestoredCallback = function (dotNetRef, methodName) {
  12.     window.connectionCallbacks = window.connectionCallbacks || [];
  13.     window.connectionCallbacks.push({ dotNetRef, methodName });
  14. };
  15.  
  16. (() => {
  17.     const maximumRetryCount = 3;
  18.     const retryIntervalMilliseconds = 1000 * 3;
  19.     const reconnectModal = document.getElementById('reconnect-modal');
  20.  
  21.     const startReconnectionProcess = () => {
  22.         reconnectModal.style.display = 'block';
  23.  
  24.         let isCanceled = false;
  25.  
  26.         (async () => {
  27.             for (let i = 0; i < maximumRetryCount; i++) {
  28.                 reconnectModal.innerText = `Attempting to reconnect: ${i + 1} of ${maximumRetryCount}`;
  29.  
  30.                 await new Promise(resolve => setTimeout(resolve, retryIntervalMilliseconds));
  31.  
  32.                 if (isCanceled) {
  33.                     return;
  34.                 }
  35.  
  36.                 try {
  37.                     const result = await Blazor.reconnect();
  38.                     if (!result) {
  39.                         // The server was reached, but the connection was rejected; reload the page.
  40.                         location.reload();
  41.                         return;
  42.                     }
  43.  
  44.                     // Successfully reconnected to the server.
  45.                     return;
  46.                 } catch {
  47.                     // Didn't reach the server; try again.
  48.                 }
  49.             }
  50.  
  51.             // Retried too many times; reload the page.
  52.             location.reload();
  53.         })();
  54.  
  55.         return {
  56.             cancel: () => {
  57.                 isCanceled = true;
  58.                 reconnectModal.style.display = 'none';
  59.             },
  60.         };
  61.     };
  62.  
  63.     let currentReconnectionProcess = null;
  64.  
  65.     Blazor.start({
  66.         configureSignalR: function (builder) {
  67.             builder.withServerTimeout(60 * 1000).withKeepAliveInterval(30 * 1000);
  68.         },
  69.         reconnectionHandler: {
  70.             onConnectionDown: () => currentReconnectionProcess ??= startReconnectionProcess(),
  71.             onConnectionUp: () => {
  72.                 currentReconnectionProcess?.cancel();
  73.                 currentReconnectionProcess = null;
  74.  
  75.                 // Call all registered reconnection callbacks
  76.                 if (window.connectionCallbacks && window.connectionCallbacks.length > 0) {
  77.                     window.connectionCallbacks.forEach(callback => {
  78.                         if (callback.dotNetRef && typeof callback.dotNetRef.invokeMethodAsync === 'function') {
  79.                             callback.dotNetRef.invokeMethodAsync(callback.methodName)
  80.                                 .catch(error => console.error("Error invoking reconnection callback:", error));
  81.                         }
  82.                     });
  83.                 }
  84.             }
  85.         }
  86.     });
  87. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement