// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddSingleton(typeof(MyService)); CrosDomain.AddConfigureService(services); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); HttpsRedirect.Configure(services); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { CrosDomain.CrosConfigures(app); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } //HttpsRedirect.Use(app); app.UseStaticFiles(); #region UseWebSocketsOptionsAO //var webSocketOptions = new WebSocketOptions() //{ // KeepAliveInterval = TimeSpan.FromSeconds(120),// 向客户端发送“ping”帧的频率,以确保代理保持连接处于打开状态。 默认值为 2 分钟。 // ReceiveBufferSize = 4 * 1024// 用于接收数据的缓冲区的大小。 高级用户可能需要对其进行更改,以便根据数据大小调整性能。 默认值为 4 KB。 //}; //webSocketOptions.AllowedOrigins.Add("https://client.com"); //webSocketOptions.AllowedOrigins.Add("https://www.client.com"); //app.UseWebSockets(webSocketOptions); app.UseWebSockets(); #endregion app.Use(async(context, next) => { if (context.Request.Path == "/ws") { if (context.WebSockets.IsWebSocketRequest) { WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); SocketList.Add(webSocket); await Echo(context, webSocket); } else { context.Response.StatusCode = 400; } } else if (context.Request.Path == "/all") { WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); SocketList.Add(webSocket); await EchoAll(context); } else { await next(); } }); app.UseMvc(); }