Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use SignalR 2.0 Owin pipeline with my SignalR lib

I'm looking into upgrading this library to SignalR 2.0

https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy

I want it to support the 2.0 Owin pipeline with the IAppBuilder interface, instead of using RouteCollection like SignalR 1.x did.

Problem is, how can I get the routecollection from the IAppBuilder? I need it to regiser a custom IHttpHandler that handles my custom js script (Like SignalR registers their hub script)

1.x setup of my lib looks like this

public static class SignalRConfig
{
    public static void Register(RouteCollection routes)
    {
        routes.MapHubs();
        routes.MapEventProxy<Contracts.Events.Event>();
    }
}

My goal for 2.0 config it something like this

public static class SignalRConfig
{
    public static void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
        app.MapEventProxy<Contracts.Events.Event>();
    }
}

My code that is dependent on RouteCollection looks like this

public static class RouteCollectionExtensions
{
    public static void MapEventProxy<TEvent>(this RouteCollection routes)
    {
        Bootstrapper.Init<TEvent>();

        routes.Add(new Route(
                       "eventAggregation/events",
                       new RouteValueDictionary(),
                       new RouteValueDictionary() {{"controller", string.Empty}},
                       new EventScriptRouteHandler<TEvent>()));
    }
}

edit: Looks like its very complex to get Owin to serve a request, can I use helper methods in SignalR 2.0 to register a route and a handler to that route?

update: Looks like im on the right track with this code

using Owin;
using SignalR.EventAggregatorProxy.Boostrap;
namespace SignalR.EventAggregatorProxy.Owin
{
    public static class AppBuilderExtensions
    {
        public static void MapEventProxy<TEvent>(this IAppBuilder app)
        {
            Bootstrapper.Init<TEvent>();
            app.Map("/eventAggregation/events", subApp => subApp.Use<EventScriptMiddleware<TEvent>>());
        }
    }
}

Now I just need to implement the EventScriptMiddleware

update: Last piece of the puzzle, now I just need my middleware to actually spit out the javacript, should be easy

namespace SignalR.EventAggregatorProxy.Owin
{
    public class EventScriptMiddleware<TEvent> : OwinMiddleware
    {
        public EventScriptMiddleware(OwinMiddleware next) : base(next)
        {
        }

        public override Task Invoke(IOwinContext context)
        {
            return context.Response.WriteAsync("Hello world!!");
        }
    }
}
like image 243
Anders Avatar asked Nov 12 '22 19:11

Anders


1 Answers

Final version looks like this, app builder extension

public static class AppBuilderExtensions
{
    public static void MapEventProxy<TEvent>(this IAppBuilder app)
    {
        Bootstrapper.Init<TEvent>();
        app.Map("/eventAggregation/events", subApp => subApp.Use<EventScriptMiddleware<TEvent>>());
    }
}

Invoke method in Middleware

public override Task Invoke(IOwinContext context)
{
    var response = context.Response;
    response.ContentType = "application/javascript";
    response.StatusCode = 200;

    if (ClientCached(context.Request, scriptBuildDate))
    {
        response.StatusCode = 304;
        response.Headers["Content-Length"] = "0";
        response.Body.Close();
        response.Body = Stream.Null;

        return Task.FromResult<Object>(null);
    }

    response.Headers["Last-Modified"] = scriptBuildDate.ToUniversalTime().ToString("r");
    return response.WriteAsync(js);
}

Full source code here

https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/tree/master/SignalR.EventAggregatorProxy/Owin

like image 150
Anders Avatar answered Nov 14 '22 22:11

Anders