-
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 connection. But will instead point to the root of your site.
Example
var connection = new HubConnection("http://mysite/");IF you've changed the default hub url on the server (to something other than /signalr), you can connection to that by doing:
var connection = new HubConnection("http://mysite/myhubs", appendDefaultUrl: false);Set the appendDefaultUrl to false to stop the default logic from appending /signalr to the url.
Creates a client side proxy to the hub on the server side.
- Returns an IHubProxy.
- NOTE: Breaking change, this needs to be the Short name of the hub.
Example
Server:
public class MyHub : Hub
{
public Task Send(string message)
{
return Clients.addMessage(message);
}
}Client:
IHubProxy myHub = connection.CreateProxy("MyHub");If a HubName is specified, then that takes precedence:
Server
[HubName("My")]
public class MyHub : Hub
{
public Task Send(string message)
{
return Clients.addMessage(message);
}
}Client:
IHubProxy myHub = connection.CreateProxy("My");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 other extension methods on IHubProxy that provide a cleaner way to do type conversion of incoming parameters.
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 various 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<string>("GetValue").ContinueWith(task =>
{
Console.WriteLine("Value from server {0}", task.Result);
});This indexer provides access to the state objects that can be set between server and client.
myHub["name"] = "Hello";Provides typed access to the state object on the hub.
var person = myHub.GetValue<Person>("person");
Console.WriteLine(person.Name);To use Rx with hub events, call the Observe extension method on IHubProxy:
IObservable<object[]> observable = myHub.Observe("receive");
observable.Subscribe(args => Console.WriteLine(args[0]));