I am using SignalR in version 2.1.2. I have noticed there are two public hub classes for me to use, Hub
and Hub<T>
. The former has an MSDN page which appears to be outdated and the latter lacks any MSDN page at all. I believe the MSDN documentation is not up to date with the latest version of SignalR from Nuget (which I'm using), because sources decompiled with the help of ReSharper show both of the classes inherit from HubBase
base class. The MSDN page's Inheritance Hierarchy section shows the Hub
class as inheriting from Object
and implementing IHub
and IDisposable
interfaces, however the decompiled sources reveal the aforementioned HubBase
base class, implementing the IHub
interface which in turn implements IDisposable
.
The difference between the non-generic and generic variant of the classes is that the non-generic one's Clients
property returns IHubCallerConnectionContext<dynamic>
while the generic variant returns typed IHubCallerConnectionContext<T>
.
I'd like to have my clients typed, so when I call the client methods from the hub, I'd have proper Intellisense support and strongly-typed arguments. What I struggle with, though, is how to let the Hub know that my client model's method is actually to be invoked 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 the non-generic hub, I'd just call .Notify()
or .Notify(new TestModel() { Name = "sth" })
on the dynamic
ly bound this.Context.Clients.Client(…)
or this.Context.Caller
, but with the generic class, when I call these empty methods in similar manner, the browser is not notified at all.
How do you use the generic hub class the way it's supposed to be used?
In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).
SignalR takes advantage of WebSocket, an HTML5 API that enables bi-directional communication between the browser and server. SignalR will use WebSockets under the covers when it's available, and gracefully fall back to other techniques and technologies when it isn't, while the application code remains the same.
public async Task BroadcastToUser(string data, string userId) => await Clients. User(userId). SendAsync("broadcasttouser", data); Remember that when we are sending messages to a user, they will be sent to all connections associated with that user and not just any particular connection.
I've found the answer. The MSDN documentation is not up-to-date as of yet, but the ASP .NET site offers nice SignalR tutorials and one of them covers the 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 an interface for the type argument, everything works and you get strongly-typed hub clients whose methods are correctly translated to RPCs. Here's a piece of code I tested this 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With