How to use a shared hub in SignalR

I am using SignalR in version 2.1.2. I noticed that for me there are two classes of the public hub, Hub and Hub<T> . The first has an MSDN page, which appears to be outdated, and the second does not have any MSDN page at all. I believe that the MSDN documentation is not updated with the latest version of SignalR from Nuget (which I use), because sources decompiled with ReSharper show both classes inheriting from the HubBase base class. The MSDN IHub Page Hierarchy section shows that the Hub class inherits from Object and implements the IHub and IDisposable , however decompiled sources expose the aforementioned HubBase base class by implementing the IHub interface, which turns IDisposable tools into.

The difference between the universal and universal variant of classes is that the non-generic one Clients property returns IHubCallerConnectionContext<dynamic> , while the general variant returns the typed IHubCallerConnectionContext<T> .

I would like my clients to type, so when I call client methods from a hub, I will have the correct Intellisense support and strongly typed arguments. However, I'm struggling with letting the hub know that my client model method is actually being called in the browser.

This is my TestModel class:

 public sealed class TestModel { public String Name { get; set; } public void Notify() {} public void NotifyComplex(TestModel model) {} } 

With a non-shared hub, I simply called .Notify() or .Notify(new TestModel() { Name = "sth" }) in dynamic ly bound this.Context.Clients.Client(…) or this.Context.Caller , but with by a common class when I call these empty methods in a similar way the browser is not notified at all.

How do you use the general hub class in the way it should be used?

+7
c # signalr signalr-hub
source share
1 answer

I have found the answer. The MSDN documentation has not yet been updated, but ASP.NET offers nice SignalR tutorials, and one of them covers typed hubs:

http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#stronglytypedhubs

As the example in the article shows, if you use the interface for the type argument, everything works, and you get strongly typed hub clients whose methods are correctly translated to RPC s. Here is a snippet of code that I checked with:

 public sealed class TestHub : Hub<ITestClient> { public override Task OnConnected() { this.Clients.Caller.SayHello("Hello from OnConnected!"); return base.OnConnected(); } public void Hi() { // Say hello back to the client when client greets the server. this.Clients.Caller.SayHello("Well, hello there!"); } } public interface ITestClient { void SayHello(String greeting); } 
+8
source share

All Articles