Files
CrystalConnector/CrystalConnector/Startup.cs
T
秋雨落 cb14d7ff74 ✨ Websocket receive.
Signed-off-by: 秋雨落 <i@rain.cx>
2023-09-13 09:06:54 +08:00

51 lines
1.6 KiB
C#

using CrystalConnector.Handlers;
using CrystalConnector.Utilities;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace CrystalConnector;
public class Startup
{
private IConfiguration Config { get; }
private ILogger<Startup> Logger { get; set; }
private IWebSocketHandler WebSocketHandler { get; set; }
public Startup(IConfiguration config)
{
Config = config;
}
public void ConfigureServices(IServiceCollection services)
{
var provider = services.BuildServiceProvider();
Logger = provider.GetService<ILogger<Startup>>()!;
WebSocketHandler = provider.GetService<IWebSocketHandler>()!;
}
public void Configure(IApplicationBuilder app)
{
app.UseWebSockets();
app.Use(async (context, next) =>
{
if (context.WebSockets.IsWebSocketRequest)
{
var websocket = await context.WebSockets.AcceptWebSocketAsync();
Logger.LogInformation("Got WebSocket connection from {Ip}:{Port}", context.Connection.RemoteIpAddress?.ToString(), context.Connection.RemotePort);
var taskCompletionSource = new TaskCompletionSource<WebSocketHandleResult>();
WebSocketHandler.StartHandle(context, websocket, taskCompletionSource);
await taskCompletionSource.Task;
}
else
{
await next(context);
}
});
}
}