在blazor服务器端关闭页面时如何获取事件? [英] How can I get the event while page close in blazor server-side?

查看:568
本文介绍了在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生命周期中似乎没有任何通过exit 事件/lifecycle?view=aspnetcore-3.1 rel = nofollow noreferrer> https://docs.microsoft.com/zh-CN/aspnet/core/blazor/lifecycle?view=aspnetcore-3.1

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.

您可以在 https://docs.microsoft.com/zh-CN/dotnet/api/microsoft.aspnetcore.components.server.circuits.circuithandler?view=aspnetcore-3.1

它使您能够响应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.

这篇关于在blazor服务器端关闭页面时如何获取事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆