检测客户端关闭连接Blazor

时间:2020-04-21 14:07:19

标签: blazor lifecycle

我有一个循环,整个客户端一直在页面上运行,但是我无法关闭循环,因此即使用户已经断开连接也始终保持运行状态。用户关闭连接时,是否存在运行的生命周期方法?还是有其他方法?

2 个答案:

答案 0 :(得分:2)

如果这是正在处理的单个页面,则让该页面继承OwningComponentBase(首选)或实现IDisposable,然后使用退出标志设置循环,或者这是一个循环任务集在任务上添加CancellationTokenSource和令牌。然后,当用户离开页面并撕下页面时,请覆盖Dispose方法并翻转退出标志或将令牌源设置为“已取消”,然后退出循环。

如果您需要更进一步,请参阅文档中的This Link讨论服务器端电路,这可能会有所帮助。对于客户端,如果在浏览器中运行,则dispose方法应该可以工作,但是如果您通过API启动了服务器端进程,则需要让该进程正常运行,因为会话的客户端将处于故障状态,如果您使用JWT,则令牌将拥有其自己的到期时间,并且不会涉及服务器。我建议在需要的地方建立超时以作为保障。

答案 1 :(得分:2)

我不确定仅实现IDisposable接口可以为您提供帮助,但可以实现Blazor Server circuit handler。我隐约记得在stackoverflow中曾问过一个与您类似的问题... OP尝试实现IDisposable接口,但无济于事。我已经回答了该问题一两次,然后又回答了一个问题...我正在从回答中发布代码,希望对您有所帮助...

CircuitHandlerService.cs

using Microsoft.AspNetCore.Components.Server.Circuits;
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;


namespace BlazorCircuitHandler.Services
{
  public class CircuitHandlerService : CircuitHandler 
  {
    public ConcurrentDictionary<string, Circuit> Circuits { get; set; }
    public event EventHandler CircuitsChanged;

    protected virtual void OnCircuitsChanged()
    => CircuitsChanged?.Invoke(this, EventArgs.Empty);

    public CircuitHandlerService()
    {
        Circuits = new ConcurrentDictionary<string, Circuit>();
    }

    public override Task OnCircuitOpenedAsync(Circuit circuit, 
                              CancellationToken cancellationToken)
    {
        Circuits[circuit.Id] = circuit;
        OnCircuitsChanged();
        return base.OnCircuitOpenedAsync(circuit, cancellationToken);
    }

    public override Task OnCircuitClosedAsync(Circuit circuit, 
                  CancellationToken cancellationToken)
    {
        Console.WriteLine("OnCircuitClosedAsync");
        Circuit circuitRemoved;
        Circuits.TryRemove(circuit.Id, out circuitRemoved);
        OnCircuitsChanged();
        return base.OnCircuitClosedAsync(circuit, cancellationToken);
    }

    public override Task OnConnectionDownAsync(Circuit circuit, 
                    CancellationToken cancellationToken)
    {
        Console.WriteLine("OnConnectionDownAsync");
        return base.OnConnectionDownAsync(circuit, cancellationToken);
    }

    public override Task OnConnectionUpAsync(Circuit circuit, 
        CancellationToken cancellationToken)
    {
        return base.OnConnectionUpAsync(circuit, cancellationToken);
    }

  }
 }

用法

@page "/"

@using Microsoft.AspNetCore.Components.Server.Circuits
@using BlazorCircuitHandler.Services

@inject CircuitHandler circuitHandler
@implements IDisposable



 <h1>Hello, world!</h1>

 Welcome to your new app.

<p>
 Number of Circuits: @((circuitHandler as 
    BlazorCircuitHandler.Services.CircuitHandlerService).Circuits.Count)
<ul>
    @foreach (var circuit in (circuitHandler as 
          BlazorCircuitHandler.Services.CircuitHandlerService).Circuits)
    {
        <li>@circuit.Key</li>
    }
</ul>
</p>

@code {

protected override void OnInitialized()
{
      // register event handler
    (circuitHandler as CircuitHandlerService).CircuitsChanged += 
                                          HandleCircuitsChanged;
}

public void Dispose()
{
    // unregister the event handler when the component is destroyed
    (circuitHandler as CircuitHandlerService).CircuitsChanged -= 
                                             HandleCircuitsChanged;
}

public void HandleCircuitsChanged(object sender, EventArgs args)
{
    // notify the UI that the state has changed
    InvokeAsync(() => StateHasChanged());
 }
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
   // ........ .....
   services.AddSingleton<CircuitHandler>(new CircuitHandlerService());
}