feat(battle-node): WebSocket endpoint at /socket.io/ + DI extension methods
This commit is contained in:
32
SVSim.BattleNode/Hosting/BattleNodeExtensions.cs
Normal file
32
SVSim.BattleNode/Hosting/BattleNodeExtensions.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
// SVSim.BattleNode/Hosting/BattleNodeExtensions.cs
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SVSim.BattleNode.Bridge;
|
||||
using SVSim.BattleNode.Sessions;
|
||||
|
||||
namespace SVSim.BattleNode.Hosting;
|
||||
|
||||
public static class BattleNodeExtensions
|
||||
{
|
||||
public static IServiceCollection AddBattleNode(this IServiceCollection services, Action<BattleNodeOptions>? configure = null)
|
||||
{
|
||||
var options = new BattleNodeOptions();
|
||||
configure?.Invoke(options);
|
||||
services.AddSingleton(options);
|
||||
services.AddSingleton<IBattleSessionStore, InMemoryBattleSessionStore>();
|
||||
services.AddSingleton<IMatchingBridge, MatchingBridge>();
|
||||
services.AddSingleton<BattleNodeWebSocketHandler>();
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseBattleNode(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseWebSockets();
|
||||
app.Map("/socket.io", branch => branch.Run(async ctx =>
|
||||
{
|
||||
var handler = ctx.RequestServices.GetRequiredService<BattleNodeWebSocketHandler>();
|
||||
await handler.HandleAsync(ctx);
|
||||
}));
|
||||
return app;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user