Added UseSignalR
This commit is contained in:
parent
6af6db67f4
commit
9e7513a7bd
|
|
@ -4,6 +4,7 @@ using Microsoft.AspNetCore.SignalR;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using SocketsSample.Hubs;
|
using SocketsSample.Hubs;
|
||||||
|
using SocketsSample.Protobuf;
|
||||||
|
|
||||||
namespace SocketsSample
|
namespace SocketsSample
|
||||||
{
|
{
|
||||||
|
|
@ -20,7 +21,6 @@ namespace SocketsSample
|
||||||
|
|
||||||
services.AddSingleton<ChatEndPoint>();
|
services.AddSingleton<ChatEndPoint>();
|
||||||
services.AddSingleton<ProtobufSerializer>();
|
services.AddSingleton<ProtobufSerializer>();
|
||||||
services.AddSingleton<InvocationAdapterRegistry>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// 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.UseDeveloperExceptionPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.UseSignalR(routes =>
|
||||||
|
{
|
||||||
|
routes.MapHub<Chat>("/hubs");
|
||||||
|
});
|
||||||
|
|
||||||
app.UseSockets(routes =>
|
app.UseSockets(routes =>
|
||||||
{
|
{
|
||||||
routes.MapSocketEndpoint<HubEndPoint<Chat>>("/hubs");
|
|
||||||
routes.MapSocketEndpoint<ChatEndPoint>("/chat");
|
routes.MapSocketEndpoint<ChatEndPoint>("/chat");
|
||||||
routes.MapSocketEndpoint<RpcEndpoint<Echo>>("/jsonrpc");
|
routes.MapSocketEndpoint<RpcEndpoint<Echo>>("/jsonrpc");
|
||||||
});
|
});
|
||||||
|
|
||||||
app.UseRpc(invocationAdapters =>
|
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("json", new JsonInvocationAdapter());
|
||||||
invocationAdapters.AddInvocationAdapter("line", new LineInvocationAdapter());
|
invocationAdapters.AddInvocationAdapter("line", new LineInvocationAdapter());
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>));
|
services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>));
|
||||||
services.AddSingleton(typeof(HubEndPoint<>), typeof(HubEndPoint<>));
|
services.AddSingleton(typeof(HubEndPoint<>), typeof(HubEndPoint<>));
|
||||||
services.AddSingleton(typeof(RpcEndpoint<>), typeof(RpcEndpoint<>));
|
services.AddSingleton(typeof(RpcEndpoint<>), typeof(RpcEndpoint<>));
|
||||||
|
services.AddSingleton<InvocationAdapterRegistry>();
|
||||||
|
|
||||||
return new SignalRBuilder(services);
|
return new SignalRBuilder(services);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue