Giter VIP home page Giter VIP logo

signalr-typesafeclient's Introduction

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:

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

With the two interfaces:

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

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

Then, on the client side:

Connecting

    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<IRequests, IEvents>(proxy, conn);

Hooking up events

These bindings

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

will call these methods

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

Sending Requests

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

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

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

signalr-typesafeclient's People

Contributors

i-e-b avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.