-
Notifications
You must be signed in to change notification settings - Fork 0
SignalR Client
The SignalR client works with WP7 applications as well as .net applications.
To use SignalR with a .NET application, install SignalR.Client:
Install-Package SignalR.Client
To use SignalR with a Windows Phone application, install SignalR.Client.WP7:
Install-Package SignalR.Client.WP7
After doing this, you should be able to connect to SignalR endpoints.
This documentation explains how to interact with a PersistentConnection, to see the documentation on interacting with Hubs see (Hubs).
To connect to a PersistentConnection endpoint, create a connection with the appropriate URL:
var connection = new Connection("http://mysite/echo");
Starts the connection.
- Returns a task that represents when negotiation is complete.
Example
connection.Start().ContinueWith(task =>
{
if(task.IsFaulted)
{
Console.Writeline("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success! Connected with client id {0}", connection.ClientId);
// Do more stuff here
}
});
Sends data over the connection.
- Returns a task that represents when the send operation is complete.
Example
connection.Send("Hello").ContinueWith(task =>
{
if(task.IsFaulted)
{
Console.WriteLine("Send failed {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success");
}
};
Stops the current connection.
- Raises the connection.Closed event.
- Sets IsActive to false.
Example
connection.Stop();
Triggered when data is received from the connection.
- The string will be in JSON format.
Example
connection.Received += data =>
{
Console.WriteLine(data);
};
Triggered when there is an error. Note that the connection is still active even after errors occur. To stop the connection, use conncection.Stop().
connection.Error += ex =>
{
Console.WriteLine("An error occurred {0}", ex.Message);
};
Triggered when the connection is closed. Calling stop will trigger this event.
connection.Closed += () =>
{
Console.WriteLine("Connection with client id {0} closed", connection.ClientId);
};
- Gets a boolean value indicating if connection is still active.
- Gets or sets the client Id for the current connection.
- Gets or sets the message id for the current connection.
We also have an IObserable implementable for the connection object. To use it just call the AsObservable extension method.
- Note, to use the Rx Linq extension methods you need to install the NuGet package for Reactive Extensions
Example
IObservble<string> observable = connection.AsObservable();
observable.Subscribe(data => Console.WriteLine(data));