问题描述
我正在 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.
通过
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 服务器连接的主干.
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(电路,CancellationToken)
在与客户端的连接断开时调用.
OnConnectionDownAsync(Circuit, CancellationToken)
Invoked when a connection to the client was dropped.
OnConnectionUpAsync(电路、CancellationToken)在建立到客户端的连接时调用.- 此方法最初在 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.
这篇关于如何在 blazor 服务器端关闭页面时获取事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!