在blazor服务器端关闭页面时如何获取事件?
我正在通过服务器的服务器端创建一个聊天室应用。
I am making a chatroom App by the blazor server-side.
我想显示每个用户的在线状态。
I want to show the online state of each user.
现在,我可以使用 OnAfterRenderAsync
事件来使用户进入页面。
Now I can use the OnAfterRenderAsync
event to get a user has entered the page.
在blazor生命周期中似乎没有任何通过
It seems there is not any exit
event in blazor lifecycle via https://docs.microsoft.com/en-us/aspnet/core/blazor/lifecycle?view=aspnetcore-3.1
有人说我可以使用 Dispose
事件来实现它,尽管它确实可以工作。
Someone said I can use the Dispose
event to achieve it while it does work at all.
此外,我有一个疯狂的想法,就是使用JS的 window.onbeforeunload
事件来调用blazor方法。
What's more, I have a crazy idea that using the window.onbeforeunload
event of js to invoke the blazor method.
我不知道哪个是最好的。你能给我一个建议吗?谢谢。
I have no idea which one is best. Would you please give me a suggestion? Thank you.
您应该为此在服务器上实现 CircuitHandler 。
You should implement a CircuitHandler on the server for this.
它使您能够响应Circuit的生命周期事件,Circuit是Blazor Server连接的中枢。
It enables you to react to lifecycle events for the Circuit, which is the backbone of a Blazor Server connection.
OnCircuitClosedAsync (电路,CancellationToken)
丢弃新电路时调用。
OnCircuitClosedAsync(Circuit, CancellationToken)
Invoked when a new circuit is being discarded.
OnCircuitOpenedAsync (电路,CancellationToken)
建立新电路时调用。
OnCircuitOpenedAsync(Circuit, CancellationToken)
Invoked when a new circuit was established.
OnConnectionDownAsync (电路,取消令牌)
断开与客户端的连接时调用。
OnConnectionDownAsync(Circuit, CancellationToken)
Invoked when a connection to the client was dropped.
OnConnectionUpAsync (电路,取消令牌)
当连接到客户端时调用客户建立了。
-此方法最初在OnCircuitOpenedAsync(Circuit,CancellationToken)之后执行一次,并且在电路的生命周期中每次重新连接都执行一次。
OnConnectionUpAsync(Circuit, CancellationToken) Invoked when a connection to the client was established. - This method is executed once initially after OnCircuitOpenedAsync(Circuit, CancellationToken) and once each for each reconnect during the lifetime of a circuit.