Passing strongly typed Hubs in SignalR

前端 未结 4 1548
夕颜
夕颜 2021-02-05 15:59

I\'ve just updated some SignalR references and things have changed somewhat in order to allow for generically typed Hubs Hub. In the existing examples and

相关标签:
4条回答
  • 2021-02-05 16:25

    Or take look at:

    https://github.com/Gandalis/SignalR.Client.TypedHubProxy

    It comes with more features than the TypeSafeClient mentioned above.

    0 讨论(0)
  • 2021-02-05 16:32

    Take a look at:

    https://github.com/i-e-b/SignalR-TypeSafeClient

    May be useful!

    0 讨论(0)
  • 2021-02-05 16:36

    In .NET Core Web App you can inject strongly typed signalR hub context like this

    public interface IClient
    {
        Task ReceiveMessage(string message);
    }
    
    public class DevicesHub : Hub<IClient>
    {
    }
    
    public class HomeController : ControllerBase
    {
        private readonly IHubContext<DevicesHub, IClient> _devicesHub;
    
        public HomeController(IHubContext<DevicesHub, IClient> devicesHub)
        {
            _devicesHub = devicesHub;
        }       
    
        [HttpGet]
        public IEnumerable<string> Get()
        {
           _devicesHub.Clients
              .All
              .ReceiveMessage("Message from devices.");
    
           return new string[] { "value1", "value2" };
        }
    }
    
    0 讨论(0)
  • 2021-02-05 16:47

    There is now a new overload of GetHubContext that takes two generic parameters. The first is the Hub type like before, but the second generic parameter is TClient (which is the T in Hub<T>).

    Assuming that StockTickerHub is defined as follows:

    public class TypedDemoHub : Hub<IClient>
    

    Then

    GlobalHost.ConnectionManager.GetHubContext<StockTickerHub>().Clients
    

    becomes

    GlobalHost.ConnectionManager.GetHubContext<StockTickerHub, IClient>().Clients
    

    The type returned by the new overload to GetHubContext would be IHubContext<IClient> and the Clients property would be IHubConnectionContext<IClient> instead of IHubConnectionContext<dynamic> or IHubConnectionContext<StockTickerHub>.

    0 讨论(0)
提交回复
热议问题