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.
|
|
#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;
namespace Apewer.Web {
internal sealed class KestrelStartup {
public KestrelStartup(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) { if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
var wsOptions = new WebSocketOptions(); wsOptions.KeepAliveInterval = TimeSpan.FromSeconds(20); app.UseWebSockets(wsOptions);
// WebSocket
//app.Use(async (context, next) =>
//{
// var server = Kestrel.Get(context.Connection.LocalPort);
// if (server != null)
// {
// var wsa = server.WebSocket;
// if (wsa != null)
// {
// if (context.WebSockets.IsWebSocketRequest)
// {
// try
// {
// using (var ws = await context.WebSockets.AcceptWebSocketAsync())
// {
// wsa.Invoke(context, ws);
// }
// }
// catch { }
// }
// }
// }
// await next();
//});
app.Run((context) => { var server = AspNetCore.Get(context.Connection.LocalPort); if (server == null) return Task.CompletedTask;
var wsa = server.WebSocket; if (wsa != null && context.WebSockets.IsWebSocketRequest) { try { using (var ws = context.WebSockets.AcceptWebSocketAsync()) { wsa.Invoke(context, ws.Result); } } catch { } return Task.CompletedTask; }
var a = server.Context; if (a != null) { try { a.Invoke(context); } catch { } return Task.CompletedTask; }
return Task.CompletedTask; });
// app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); });
}
}
}
#endif
|