You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.5 KiB

#if NETCORE
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using System.Threading.Tasks;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Hosting.Server;
namespace Apewer.Web
{
internal sealed class HttpSysStartup
{
public HttpSysStartup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// 此方法由运行时调用。
// 可使用此方法添加服务到容器。
public void ConfigureServices(IServiceCollection services)
{
// services.AddControllers();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
// 此方法由运行时调用。
// 可使用此方法配置 HTTP 请求命名管道。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<HttpSysStartup> logger, IServer server)
{
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
var wsOptions = new WebSocketOptions();
wsOptions.KeepAliveInterval = TimeSpan.FromSeconds(20);
app.UseWebSockets(wsOptions);
app.Use(async (context, next) =>
{
var server = AspNetCore.Get(context.Connection.LocalPort);
if (server == null) return;
var wsa = server.WebSocket;
if (wsa != null && context.WebSockets.IsWebSocketRequest)
{
try
{
using (var ws = context.WebSockets.AcceptWebSocketAsync())
{
wsa.Invoke(context, ws.Result);
}
}
catch { }
return;
}
var a = server.Context;
if (a != null)
{
try
{
a.Invoke(context);
}
catch { }
return;
}
await next.Invoke();
});
}
}
}
#endif