简体   繁体   English

SignalR 注入的集线器似乎在一段时间后丢失了组

[英]SignalR injected hub seems to lose groups after a period

Core 3.1 application核心 3.1 应用

Totally at loss here.在这里完全不知所措。

I have 2 clients (phone and tablet) which connect to a signalR hub where I register them each as a group:我有 2 个客户端(手机和平板电脑)连接到 signalR 集线器,我将它们分别注册为一个组:

   public class OrderHub : Hub
    {
        public string GetConnectionId()
        {
            return Context.ConnectionId;
        }

        public async void RegisterDeviceOnGroup() { //for example purposes just using tablet
           
          await Groups.AddToGroupAsync(Context.ConnectionId, 'tablet');
            
        }
    }

Then when I communicate between the 2, I call a method on a controller where the hub as been injected:然后,当我在 2 之间进行通信时,我在注入集线器的控制器上调用一个方法:

private readonly IHubContext<OrderHub> _hubContext;

public CustomerRepository(IHubContext<OrderHub> hubContext)
{
  _hubContext = hubContext;
}

private async Task<bool> BroadcastOrder() { //Broadcast to tablet
   await _hubContext.Clients.Group('tablet).SendAsync("Message");
}

This works fine for a few minutes and then stops.这可以正常工作几分钟,然后停止。 I can 't see anything in logs or any reason why it would.我在日志中看不到任何内容,也看不到任何原因。

Can the injected hub context lose groups?注入的集线器上下文会丢失组吗?

I guess the problem is in sending parameters , naming ...我想问题在于发送参数,命名......

However, you can use Custom hub filters to view the communication between the client and the server但是,您可以使用自定义集线器过滤器来查看客户端和服务器之间的通信

public class CustomFilter : IHubFilter
    {
        public async ValueTask<object> InvokeMethodAsync(
            HubInvocationContext invocationContext, Func<HubInvocationContext, ValueTask<object>> next)
        {
            Console.WriteLine($"Calling hub method '{invocationContext.HubMethodName}'");
            try
            {
                return await next(invocationContext);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception calling '{invocationContext.HubMethodName}': {ex}");
                throw new HubException(ex.Message);
                throw;
            }
        } 
        // Optional method
        public Task OnConnectedAsync(HubLifetimeContext context, Func<HubLifetimeContext, Task> next)
        {
            return next(context);
        } 
        // Optional method
        public Task OnDisconnectedAsync(
            HubLifetimeContext context, Exception exception, Func<HubLifetimeContext, Exception, Task> next)
        {
            return next(context, exception);
        }
    }

and add IServiceCollection并添加 IServiceCollection

hubOptions.AddFilter<CustomFilter>();

you can see enter link description here你可以在这里看到输入链接描述

I hope that help我希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM