Coder Social home page Coder Social logo

killnine / hubconnectionmanager Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 12.71 MB

SignalR extension to help retry connecting to server if initialization fails on startup. Also provides more convenient unit testing interface.

C# 97.68% Shell 2.32%

hubconnectionmanager's Introduction

##HubConnectionManager##

###Overview###

HubConnectionManager is a simple wrapper class around the HubConnection class SignalR uses to establish a connection with a server.

###NuGet Installation### > Install-Package HubConnectionManager

https://www.nuget.org/packages/HubConnectionManager/

###Background###

Prior to HubConnectionManager, if I happened to launch a client applicaiton when its corresponding SignalR service was unavailable, I'd have to restart the whole application for it to recover. Now, with HubConnectionManager, I can ensure that the client will attempt to re-establish a connection with the server. And it works!

Furthermore, wrapping the HubConnection in an IHubConnectionManager interface means that mocking connection state is easy. Hooray for comprehensive unit testing!

###Usage###

  1. Create an instance of HubConnectionManager through the static GetHubConnectionManager method, providing the url of your SignalR endpoint:
HubConnectionManager connectionManager = HubConnectionManager.GetHubConnectionManager("http://localhost:6789");
  1. Create you IHubProxy via the CreateHubProxy method, passing in the name of your Hub:
IHubProxy clientProxy = connectionManager.CreateHubProxy("ClientHub");
  1. Initialize the Manager via Initialize() method:
connectionManager.Initialize();

That's it! As long as you have a reference to the HubConnectionManager like you'd have a reference to your HubConnection in the past, it should work just the same.

###Additional Functionality###

I also expose many of the events and properties of HubConnection:

####Events####

  • Error
  • Received
  • Closed
  • Reconnecting
  • Reconnected
  • ConnectionSlow
  • StateChanged

####Properties####

  • State

hubconnectionmanager's People

Contributors

jamiehumphries avatar killnine avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

martonboda

hubconnectionmanager's Issues

Client Disconnect (by choice!)

As of 1.0.2, the Manager will always try and reconnect any time that it detects the Closed event. Probably need to prevent this. Users may want to eventually disconnect ;)

SignalR.Server closes unexpectedly on restart

When running SignalR.Server and SignalR.Client together for testing, it seems like the server will run fine for a while, but if I restart the server to test the Client's ability to reconnect, the Server will unexpectedly shut down after a short period.

Not sure if I am fat-fingering a key and causing it to close, or something is being disposed prematurely and causing the application to crash.

Extension Method for converting an OAuth access token to a cookie, then adding it to the Hub Connection

Not sure how helpful this is/will be:

    public static class HubConnectionExtensions {
        public async static void WithOAuthToken(this HubConnection connection, Uri authUri, string accessToken) {
            var cookies = new CookieContainer();
            var clientHandler = new System.Net.Http.HttpClientHandler { 
                UseCookies = true, 
                CookieContainer = cookies 
            };
            var client = new HttpClient(clientHandler) {
                BaseAddress = new Uri(connection.Url)
            };
            client.SetBearerToken(accessToken);
            var result = await client.GetAsync(authUri);
            result.EnsureSuccessStatusCode();

            connection.CookieContainer = cookies;
        }
    }

Resubscribing to groups

Looking for input on what happens after a disconnect (not just connection interruption, but after the DisconnectTimeout period ends and the client disconnects).

According to ASP.NET documentation, all Group subscriptions are lost after a disconnect. Just looking for input on what the best course of action should be on the part of the HubConnectionManager after this happens.

Should it facilitate re-subscribing to those groups after connection is re-established?

Currently, I am doing this manually on the client by subscribing to the StateChanged event and calling a method on the Hub that subscribes again to the appropriate groups.

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.