将 SignalR 2.0 Owin 管道与我的 SignalR 库一起使用

本文关键字:SignalR 一起 我的 管道 Owin | 更新日期: 2023-09-27 18:37:20

我正在考虑将此库升级到 SignalR 2.0

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

我希望它支持带有IAppBuilder接口的 2.0 Owin 管道,而不是像 SignalR 1.x 那样使用 RouteCollection

问题是,如何从 IAppBuilder 获取路由集合?我需要它来注册处理我的自定义 js 脚本的自定义 IHttpHandler(就像 SignalR 注册他们的中心脚本一样)

我的库的 1.x 设置如下所示

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

我对 2.0 的目标配置如下

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

依赖于路由集合的代码如下所示

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>()));
    }
}

编辑:看起来让 Owin 为请求提供服务非常复杂,我可以在 SignalR 2.0 中使用帮助程序方法来注册该路由的路由和处理程序吗?

更新:看起来我使用此代码走在正确的轨道上

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>>());
        }
    }
}

现在我只需要实现EventScriptMiddleware

更新:最后一块拼图,现在我只需要我的中间件来实际吐出javacript,应该很容易

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!!");
        }
    }
}

将 SignalR 2.0 Owin 管道与我的 SignalR 库一起使用

最终版本如下所示,应用程序构建器扩展

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

在中间件中调用方法

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);
}

完整的源代码在这里

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