-
Notifications
You must be signed in to change notification settings - Fork 0
PersistentConnection
A PersistentConnection is the base class that has an api for exposing a SignalR service over http.
To create a new endpoint. Create a new class that derives from PersistentConnection.
public class MyEndPoint : PersistentConnection
{
}To make this endpoint accessible, add a route to Application_Start (or something similar):
using SignalR;
public void Application_Start()
{
RouteTable.Routes.MapConnection<MyEndPoint>("echo", "echo/{*operation}");
}This will expose /echo as a SignalR endpoint.
Called when a new connection is made.
- request - The request associated with this connection.
- connectionId - The id of the new connection.
Example
public class MyEndPoint : PersistentConnection
{
protected override Task OnConnectedAsync(IRequest request, string connectionId)
{
return Connection.Broadcast("Connection " + connectionId + " connected");
}
}Called when a connection is restored.
- request - The request associated with this connection.
- groups - A list of groups the client is a part of.
- connectionId - The id of the new connection.
Example
public class MyEndPoint : PersistentConnection
{
protected override Task OnReconnectedAsync(IRequest request, IEnumerable<string> groups, string clientId)
{
return Connection.Broadcast("Client " + clientId + " re-connected");
}
}Called when data is received on the connection.
- request - The request associated with the connection that sent the data.
- connectionId - The id of the connection that sent the data.
- data - The payload received from the client.
Example
public class MyEndPoint : PersistentConnection
{
protected override Task OnReceivedAsync(IRequest request, string connectionId, string data)
{
return Connection.Broadcast("Connection " + connectionId+ " sent " + data);
}
}Called when a connection goes away (client is no longer connected, e.g. browser close).
- connectionId - The id of the client that disconnected
Example
public class MyEndPoint : PersistentConnection
{
protected override Task OnDisconnectAsync(string connectionId)
{
return Connection.Broadcast("Connection " + connectionId+ " disconncted");
}
}Called when an error occurs on the connection.
- error - The error that occurred.
Example
public class MyEndPoint : PersistentConnection
{
protected override Task OnErrorAsync(Exception error)
{
return Connection.Broadcast("Error ocurred " + error);
}
}Sends a message to the specified client.
- clientId - Client id to send the message to
- data - The payload to send
- NOTE: Objects will be JSON serialized
Example
public class MyEndPoint : PersistentConnection
{
protected override Task OnReceivedAsync(string clientId, string data)
{
return Send(clientId, data);
}
}
Adds a client to the specified group.
- clientId - client id to add to the group
- groupName - name of the group to add the client to
- NOTE: The task returned from this method does not mean the client was successfully added to the group.
Example
public class MyEndPoint : PersistentConnection
{
protected override Task OnReceivedAsync(string clientId, string data)
{
return AddToGroup(clientId, "foo");
}
}
Removes a client from the specified group.
- clientId - client id to remove from the group
- groupName - name of the group to remove the client from
- NOTE: The task returned from this method does not mean the client was successfully removed from the group.
Example
public class MyEndPoint : PersistentConnection
{
protected override Task OnReceivedAsync(string clientId, string data)
{
return RemoveFromGroup(clientId, "foo");
}
}
Sends a message to the specified group.
- groupName - name of the group to send the message to
- value - The payload to send
- NOTE: Objects will be JSON serialized
Example
public class MyEndPoint : PersistentConnection
{
protected override Task OnReceivedAsync(string clientId, string data)
{
return SendToGroup("foo", data);
}
}
- Gets the IConnection for this PersistentConnection.
Broadcasts a message to all on this connection.
- NOTE: the value is JSON serialized
Sometimes you have some arbitrary code in an application that you want to be able to notify all clients connected when some event occurs. An example of this might be a background task that sends data at a certain interval.
You can use the dependency resolver for the current host to resolve the IConnectionManager interface which allows you to get ahold of the connection object.
In these scenarios, you can get an IConnection for a specific persistent connection by calling connectionManager.GetConnectionContext<T>:
using SignalR;
var context = GlobalHost.ConnectionManager.GetConnectionContext<MyEndPoint>();
context.Connection.Broadcast("Hello from outside of the connection!");Once you have the connection, you can call Broadcast to send a message to all clients.