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#
- Host: GitHub
- URL: https://github.com/i-e-b/signalr-typesafeclient
- Owner: i-e-b
- License: bsd-3-clause
- Created: 2014-04-02T14:51:58.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2016-08-01T23:36:32.000Z (almost 10 years ago)
- Last Synced: 2024-04-14T19:49:39.172Z (about 2 years ago)
- Topics: c-sharp, messaging, old, signalr, working
- Language: C#
- Homepage:
- Size: 262 KB
- Stars: 11
- Watchers: 5
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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());
```