Decouple the dispatcher from routing for easier testing

This commit is contained in:
David Fowler 2016-10-04 00:12:07 -07:00
parent 411f44f263
commit e5e8d1bee3
3 changed files with 29 additions and 24 deletions

View File

@ -31,11 +31,11 @@ namespace SocketsSample
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }
app.UseSockets(d => app.UseSockets(routes =>
{ {
d.MapSocketEndpoint<HubEndpoint>("/hubs"); routes.MapSocketEndpoint<HubEndpoint>("/hubs");
d.MapSocketEndpoint<ChatEndPoint>("/chat"); routes.MapSocketEndpoint<ChatEndPoint>("/chat");
d.MapSocketEndpoint<JsonRpcEndpoint>("/jsonrpc"); routes.MapSocketEndpoint<JsonRpcEndpoint>("/jsonrpc");
}); });
} }
} }

View File

@ -2,10 +2,7 @@
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Channels; using Channels;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Sockets.Routing;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
@ -15,19 +12,6 @@ namespace Microsoft.AspNetCore.Sockets
{ {
private readonly ConnectionManager _manager = new ConnectionManager(); private readonly ConnectionManager _manager = new ConnectionManager();
private readonly ChannelFactory _channelFactory = new ChannelFactory(); private readonly ChannelFactory _channelFactory = new ChannelFactory();
private readonly RouteBuilder _routes;
public HttpConnectionDispatcher(IApplicationBuilder app)
{
_routes = new RouteBuilder(app);
}
public void MapSocketEndpoint<TEndPoint>(string path) where TEndPoint : EndPoint
{
_routes.AddPrefixRoute(path, new RouteHandler(c => Execute<TEndPoint>(path, c)));
}
public IRouter GetRouter() => _routes.Build();
public async Task Execute<TEndPoint>(string path, HttpContext context) where TEndPoint : EndPoint public async Task Execute<TEndPoint>(string path, HttpContext context) where TEndPoint : EndPoint
{ {

View File

@ -1,19 +1,40 @@
using System; using System;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Sockets; using Microsoft.AspNetCore.Sockets;
using Microsoft.AspNetCore.Sockets.Routing;
namespace Microsoft.AspNetCore.Builder namespace Microsoft.AspNetCore.Builder
{ {
public static class HttpDispatcherAppBuilderExtensions public static class HttpDispatcherAppBuilderExtensions
{ {
public static IApplicationBuilder UseSockets(this IApplicationBuilder app, Action<HttpConnectionDispatcher> callback) public static IApplicationBuilder UseSockets(this IApplicationBuilder app, Action<SocketRouteBuilder> callback)
{ {
var dispatcher = new HttpConnectionDispatcher(app); var dispatcher = new HttpConnectionDispatcher();
callback(dispatcher); var routes = new RouteBuilder(app);
callback(new SocketRouteBuilder(routes, dispatcher));
// TODO: Use new low allocating websocket API // TODO: Use new low allocating websocket API
app.UseWebSockets(); app.UseWebSockets();
app.UseRouter(dispatcher.GetRouter()); app.UseRouter(routes.Build());
return app; return app;
} }
public class SocketRouteBuilder
{
private readonly HttpConnectionDispatcher _dispatcher;
private readonly RouteBuilder _routes;
public SocketRouteBuilder(RouteBuilder routes, HttpConnectionDispatcher dispatcher)
{
_routes = routes;
_dispatcher = dispatcher;
}
public void MapSocketEndpoint<TEndPoint>(string path) where TEndPoint : EndPoint
{
_routes.AddPrefixRoute(path, new RouteHandler(c => _dispatcher.Execute<TEndPoint>(path, c)));
}
}
} }
} }