From 9e7513a7bd6b88f170db759c38a858b5bfcb8609 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Wed, 2 Nov 2016 03:20:44 -0700 Subject: [PATCH] Added UseSignalR --- samples/SocketsSample/Startup.cs | 10 +++-- .../DependencyInjectionExtensions.cs | 1 + .../SignalRAppBuilderExtensions.cs | 38 +++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs diff --git a/samples/SocketsSample/Startup.cs b/samples/SocketsSample/Startup.cs index 05afcc881d..cca6789713 100644 --- a/samples/SocketsSample/Startup.cs +++ b/samples/SocketsSample/Startup.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SocketsSample.Hubs; +using SocketsSample.Protobuf; namespace SocketsSample { @@ -20,7 +21,6 @@ namespace SocketsSample services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -35,16 +35,20 @@ namespace SocketsSample app.UseDeveloperExceptionPage(); } + app.UseSignalR(routes => + { + routes.MapHub("/hubs"); + }); + app.UseSockets(routes => { - routes.MapSocketEndpoint>("/hubs"); routes.MapSocketEndpoint("/chat"); routes.MapSocketEndpoint>("/jsonrpc"); }); app.UseRpc(invocationAdapters => { - invocationAdapters.AddInvocationAdapter("protobuf", new Protobuf.ProtobufInvocationAdapter(app.ApplicationServices)); + invocationAdapters.AddInvocationAdapter("protobuf", new ProtobufInvocationAdapter(app.ApplicationServices)); invocationAdapters.AddInvocationAdapter("json", new JsonInvocationAdapter()); invocationAdapters.AddInvocationAdapter("line", new LineInvocationAdapter()); }); diff --git a/src/Microsoft.AspNetCore.SignalR/DependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR/DependencyInjectionExtensions.cs index 125372ee6c..a5942a94ad 100644 --- a/src/Microsoft.AspNetCore.SignalR/DependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/DependencyInjectionExtensions.cs @@ -14,6 +14,7 @@ namespace Microsoft.Extensions.DependencyInjection services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>)); services.AddSingleton(typeof(HubEndPoint<>), typeof(HubEndPoint<>)); services.AddSingleton(typeof(RpcEndpoint<>), typeof(RpcEndpoint<>)); + services.AddSingleton(); return new SignalRBuilder(services); } diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs new file mode 100644 index 0000000000..7f09d5c8d0 --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; +using static Microsoft.AspNetCore.Builder.HttpDispatcherAppBuilderExtensions; + +namespace Microsoft.AspNetCore.Builder +{ + public static class SignalRAppBuilderExtensions + { + public static IApplicationBuilder UseSignalR(this IApplicationBuilder app, Action configure) + { + // REVIEW: Should we discover hubs? + app.UseSockets(routes => + { + configure(new HubRouteBuilder(routes)); + }); + + return app; + } + } + + public class HubRouteBuilder + { + private readonly SocketRouteBuilder _routes; + + public HubRouteBuilder(SocketRouteBuilder routes) + { + _routes = routes; + } + + public void MapHub(string path) where THub : Hub + { + _routes.MapSocketEndpoint>(path); + } + } +}