-
Notifications
You must be signed in to change notification settings - Fork 0
SignalR Client Hubs
See SignalR Client for instructions on how to get started with SignalR.Client.
To connect to a hub using SignalR, create a HubConnection with the appropriate url.
- Note: This url will not point to a specific handler. But will instead point to the root of your site.
Example
var connection = new HubConnection("http://mysite/");
Creates a client side proxy to the hub on the server side.
- Returns an IHubProxy.
- NOTE: The name of this hub needs to be the full type name of the hub.
Example
IHubProxy myHub = connection.CreateProxy("MySite.MyHub");
Starts the connection for all hubs.
The hub proxy object can be used to subscribe to events and invoke methods on the server side hub. To create a hub proxy, use HubConnection.CreateProxy.
Subscribes for notifications on the specified event.
- Returns a Subscription object.
- NOTE: Calling create proxy with the same name will return the same Subscription object.
- NOTE: This API isn't the recommended way to subscribe to events on a hub. There are a bunch of extension methods on IHubProxy that provide a cleaner way to do type conversion of incoming parameters.
Example
Subscription sub = myHub.Subscribe("receive");
sub.Data += args =>
{
Console.WriteLine(args[0]);
};
To make subscribing to events on hubs easy and convenient, the On methods were created. These provide a clean API for subscribing to events on the hub.
Subscribes to the event name and calls triggers onData when a messages for the specified event name is received.
myHub.On("notify", () => Console.WriteLine("notified!"));
Subscribes to the event name and calls triggers onData when a messages for the specified event name is received.
myHub.On("receive", data => Console.WriteLine("Received data {0}", data));
Subscribes to the event name and calls triggers onData when a messages for the specified event name is received.
- NOTE: There are a bunch of generic overloads to On which allow specifying up to 8 parameters (4 on WP7).
Example
myHub.On<string>("receive", data => Console.WriteLine("Received data {0}", data));
Invokes a server side hub method.
- Returns a task that represents the completion of the method call on the server.
Example
myHub.Invoke("MethodOnTheServer", 10, new { Name = "SignalR" }).ContinueWith(task =>
{
if(task.IsFaulted)
{
Console.WriteLine("An error occurred during the method call {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Successfully called MethodOnServer");
}
});
Invokes a server side hub method.
- Returns a generic task with the return value of the hub.
Example
myHub.Invoke("GetValue").ContinueWith(task =>
{
Console.WriteLine("Value from server {0}", task.Result);
});
A subscription can be used to add or remove event handlers for a specific hub event. Create new subscriptions with hubProxy.Subscribe.
Triggered when data is available for the Subscription.
- The arguments in the event handers represent arguments from the server side triggering of the event.
- The data at each argument index will be a JSON object.
Example
subscription.Data += args =>
{
dynamic person = args[0];
Console.WriteLine(person.Name);
};