Added UseSignalR

This commit is contained in:
David Fowler 2016-11-02 03:20:44 -07:00
parent 6af6db67f4
commit 9e7513a7bd
3 changed files with 46 additions and 3 deletions

View File

@ -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<ChatEndPoint>();
services.AddSingleton<ProtobufSerializer>();
services.AddSingleton<InvocationAdapterRegistry>();
}
// 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<Chat>("/hubs");
});
app.UseSockets(routes =>
{
routes.MapSocketEndpoint<HubEndPoint<Chat>>("/hubs");
routes.MapSocketEndpoint<ChatEndPoint>("/chat");
routes.MapSocketEndpoint<RpcEndpoint<Echo>>("/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());
});

View File

@ -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<InvocationAdapterRegistry>();
return new SignalRBuilder(services);
}

View File

@ -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<HubRouteBuilder> 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<THub>(string path) where THub : Hub
{
_routes.MapSocketEndpoint<HubEndPoint<THub>>(path);
}
}
}