简体   繁体   中英

Passing strongly typed Hubs in SignalR

I've just updated some SignalR references and things have changed somewhat in order to allow for generically typed Hubs Hub<T> . In the existing examples and documentation such as at:

Server-Broadcast-with-Signalr

we have a static class holding references to clients via the following mechanisms:

public class StockTicker()
{
private readonly static Lazy<StockTicker> _instance = new Lazy<StockTicker>(
            () => new StockTicker(GlobalHost.ConnectionManager.GetHubContext<StockTickerHub>().Clients));

 IHubConnectionContext Clients {get;set;}

 private StockTicker(IHubConnectionContext clients)
        {
            Clients = clients;
        }
}

So a static reference is checked and if null it reaches out to :

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

to create an instance and supply the clients via the constructor.

So this was how it used to work and indeed is how the url above has it. But now with Hub<T> there is a slight change required in the constructor:

 private StockTicker(IHubConnectionContext<dynamic> clients)
 {
   Clients = clients;
 }

Now my question is how can I extend this further such that my version of StockTicker can have a strongly typed property for clients of type x.

 IHubConnectionContext<StockTickerHub> Clients {get;set;}

 private StockTicker(IHubConnectionContext<dynamic> clients)
 {
   Clients = clients; // Fails, wont compile
 }

By maintaining strongly typed references I would be able to call strongly typed methods, etc.

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> .

Or take look at:

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

It comes with more features than the TypeSafeClient mentioned above.

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" };
    }
}

Take a look at:

https://github.com/ieb/SignalR-TypeSafeClient

May be useful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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