Skip to content
davidfowl edited this page Nov 5, 2011 · 38 revisions

To use SignalR with a .NET application, first install the package:

Install-Package SignalR.Client

After doing this, you should be able to connect to SignalR endpoints.

Connection

To connect to a PersistentConnection endpoint, create a connect with the appropriate URL:

var connection = new Connection("http://mysite/echo");

Task Connection.Start()

Starts the connection.

  • Returns a task that represents when negotiation is complete.

A 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 } });

Task Connection.Send(string)

Sends data over the connection.

  • Returns a task that represents when the send operation is complete.

    connection.Send("Hello").ContinueWith(task => { if(task.IsFaulted) { Console.WriteLine("Send failed {0}", task.Exception.GetBaseException()); } else { Console.WriteLine("Success"); } };

void Connection.Stop

Stops the current connection. Raises the connection.Closed event and sets IsActive to false.

connection.Stop();

Connection.Received (event Action<string>)

Triggered when data is received from the connection.

  • The string will be in JSON format.

    connection.Received += data => { Console.WriteLine(data); };

Connection.Error (event Action<Exception>)

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);
};

Connection.Closed (event Action)

Triggered when the connection is closed. Calling stop will trigger this event.

connection.Closed += () =>
{
    Console.WriteLine("Connection with client id {0} closed", connection.ClientId);
};

bool Connection.IsActive

  • Gets a boolean value indicating if connection is still active.

string Connection.ClientId

  • Gets or sets the client Id for the current connection.

long? Connection.MessageId

  • Gets or sets the message is id for the current connection.

Clone this wiki locally