Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
861 views
in Technique[技术] by (71.8m points)

asp.net core - How can I get the event while page close in blazor server-side?

I am making a chatroom App by the blazor server-side.

I want to show the online state of each user.

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

Someone said I can use the Dispose event to achieve it while it does work at all.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should implement a CircuitHandler on the server for this.

You can find the documentation here https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.server.circuits.circuithandler?view=aspnetcore-3.1

It enables you to react to lifecycle events for the Circuit, which is the backbone of a Blazor Server connection.

Methods

OnCircuitClosedAsync(Circuit, CancellationToken)
Invoked when a new circuit is being discarded.

OnCircuitOpenedAsync(Circuit, CancellationToken)
Invoked when a new circuit was established.

OnConnectionDownAsync(Circuit, CancellationToken)
Invoked when a connection to the client was dropped.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...