SignalR injected hub seems to lose groups after a period

Viewed 43

Core 3.1 application

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:

   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:

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?

1 Answers

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

hubOptions.AddFilter<CustomFilter>();

you can see enter link description here

I hope that help

Related