#components-reconnect-modal預設行為狀態有四種,以分別用不同的 Css Class Name 顯示:
SS class
Indicates…
components-reconnect-hide
連線正常
An active connection is re-established to the server. Hide the modal.
components-reconnect-show
連線不正常-嘗試重新連線,可能是網路跳針有機會連回並接續操作。
A lost connection. The client is attempting to reconnect. Show the modal.
components-reconnect-failed
連線不正常-嘗試連線失敗,大概率連接不回來了。
Reconnection failed, probably due to a network failure. To attempt reconnection, call window.Blazor.reconnect() in JavaScript.
components-reconnect-rejected
連線不正常-無法連線,只能重新載入頁面。
Reconnection rejected. The server was reached but refused the connection, and the user's state on the server is lost. To reload the app, call location.reload() in JavaScript. This connection state may result when:
A crash in the server-side circuit occurs.
The client is disconnected long enough for the server to drop the user's state. Instances of the user's components are disposed.
The server is restarted, or the app's worker process is recycled.
也就是正常 SignalR 連線狀態時,
不正常連線狀態時,Blazor 引擎會為 #components-reconnect-modal加上不正常狀態的 Css Class Name 。
簡化連線不正常操作
我們直接簡化連線不正常操作成一種就好,紀錄如下:
預設行為:SignalR 連線正常時不會出現,不正常會自動 append 到網頁最尾端。
改寫行為時,要自訂 #components-reconnect-modal 呈現內容,Blazor 引擎會為 #components-reconnect-modal加上不正常狀態的 Css Class Name 。我們只要用 CSS 控制當 Signal R 連線不正常時再讓它顯現就好,平常就隱身。
<div id="components-reconnect-modal" class="components-reconnect-hide">
There was a problem with the connection!
</div>
<div id="components-reconnect-modal" class="components-reconnect-show">
There was a problem with the connection!
</div>
<!-- 或 -->
<div id="components-reconnect-modal" class="components-reconnect-failed">
There was a problem with the connection!
</div>
<!-- 或 -->
<div id="components-reconnect-modal" class="components-reconnect-rejected">
There was a problem with the connection!
</div>
_Layout.cshtml
@using Microsoft.AspNetCore.Components.Web
@namespace YourProject.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html lang="zh-hant">
<head>
...略...
</head>
<body>
@RenderBody()
@* 客製化:SignalR 失去連線處置 *@
<div id="components-reconnect-modal">
<p>與伺服器連線出現問題!(嘗試連線:<span id="components-reconnect-current-attempt"></span> / <span id="components-reconnect-max-retries"></span>)</p>
<button onclick="location.reload()">直接刷新頁面</button>
</div>
<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<script src="_framework/blazor.server.js"></script>
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
</body>
</html>