mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-27 05:23:13 +00:00
AspNet
This commit is contained in:
19
Esiur.AspNetCore/Esiur.AspNetCore.csproj
Normal file
19
Esiur.AspNetCore/Esiur.AspNetCore.csproj
Normal file
@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Esiur\Esiur.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
47
Esiur.AspNetCore/EsiurMiddleware.cs
Normal file
47
Esiur.AspNetCore/EsiurMiddleware.cs
Normal file
@ -0,0 +1,47 @@
|
||||
|
||||
using Esiur.Net.IIP;
|
||||
using Esiur.Net.Sockets;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Net.WebSockets;
|
||||
|
||||
namespace Esiur.AspNetCore
|
||||
{
|
||||
public class EsiurMiddleware
|
||||
{
|
||||
readonly DistributedServer server;
|
||||
readonly RequestDelegate next;
|
||||
readonly ILoggerFactory loggerFactory;
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
var buffer = new ArraySegment<byte>(new byte[10240]);
|
||||
|
||||
if (context.WebSockets.IsWebSocketRequest)
|
||||
{
|
||||
var webSocket = await context.WebSockets.AcceptWebSocketAsync("iip");
|
||||
var socket = new FrameworkWebSocket(webSocket);
|
||||
var iipConnection = new DistributedConnection();
|
||||
server.Add(iipConnection);
|
||||
iipConnection.Assign(socket);
|
||||
socket.Begin();
|
||||
|
||||
while (webSocket.State == WebSocketState.Open) ;
|
||||
}
|
||||
else
|
||||
{
|
||||
await next(context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public EsiurMiddleware(RequestDelegate next, IOptions<EsiurOptions> options, ILoggerFactory loggerFactory)
|
||||
{
|
||||
this.server = options.Value.Server;
|
||||
this.loggerFactory = loggerFactory;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
}
|
17
Esiur.AspNetCore/EsiurMiddlewareExtensions.cs
Normal file
17
Esiur.AspNetCore/EsiurMiddlewareExtensions.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Esiur.AspNetCore
|
||||
{
|
||||
public static class EsiurMiddlewareExtensions
|
||||
{
|
||||
public static IApplicationBuilder UseEsiur(this IApplicationBuilder app, EsiurOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(app);
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
return app.UseMiddleware<EsiurMiddleware>(Options.Create(options));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
9
Esiur.AspNetCore/EsiurOptions.cs
Normal file
9
Esiur.AspNetCore/EsiurOptions.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using Esiur.Net.IIP;
|
||||
|
||||
namespace Esiur.AspNetCore
|
||||
{
|
||||
public class EsiurOptions
|
||||
{
|
||||
public DistributedServer Server { get; set; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user