An open API service indexing awesome lists of open source software.

https://github.com/i-e-b/signalr-typesafeclient

A type safe client interface for SignalR in C#
https://github.com/i-e-b/signalr-typesafeclient

c-sharp messaging old signalr working

Last synced: about 1 year ago
JSON representation

A type safe client interface for SignalR in C#

Awesome Lists containing this project

README

          

SignalR-TypeSafeClient
======================

A type safe client interface for SignalR in C#

The `HubClient` uses two interface types which should be shared with your hub: *Requests* and *Events*.
Events are calls made from the Hub to the Client. Requests are calls made from the Client to the Hub.

Contracts and Hub
-----------------
For this readme, we assume a Hub like:
```csharp

[HubName("MyHub")]
public class MyHub : Hub, IRequests, IEvents
{
. . .
}

```

With the two interfaces:

```csharp

public interface IRequests {
void IWantStuff(string[] kindsOfStuff);
int HowManyClients();
}

public interface IEvents {
void StuffAvailable(string stuff);
void SessionExpired();
}

```

Then, on the client side:

Connecting
----------

```csharp

var conn = new HubConnection("http://localhost:8080/"); // replace with your Hub's location

conn.TransportConnectTimeout = TimeSpan.FromSeconds(15);
conn.TraceLevel = TraceLevels.All;
conn.TraceWriter = Console.Out

var proxy = conn.CreateHubProxy("MyHub"); // replace with your hub name or hub class name

var connection = conn.Start();

if (!connection.Wait(TimeSpan.FromSeconds(30)))
{
conn.Dispose();
throw new TimeoutException("Could not connect to " + endpoint);
}

_client = new HubClient(proxy, conn);
```

Hooking up events
-----------------
These bindings

```csharp

_client.BindEventHandler(hub => hub.StuffAvailable, HandleIncomingStuff);
_client.BindEventHandler(hub => hub.SessionExpired, AlertSessionExpired);

```

will call these methods

```csharp

void HandleIncomingStuff(string stuff) { . . . }
void AlertSessionExpired() { . . . }

```

Sending Requests
----------------

For calls that have no return use `SendToHub`. For calls requiring a synchronous result, use `RequestFromHub`.
```csharp

_client.SendToHub(hub => hub.IWantStuff(new []{"stuff1", "stuff2"}));

var clients = _client.RequestFromHub(hub => hub.HowManyClients());

```