public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseFileServer(); app.UseWebSockets(); app.Use(async(context, next) => { if (context.Request.Path == "/ws") { if (context.WebSockets.IsWebSocketRequest) { WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); await EchoHandler.Echo(webSocket); } else { context.Response.StatusCode = StatusCodes.Status400BadRequest; } } else { await next(); } }); app.Run(async(context) => { await context.Response.WriteAsync("Hello World!"); }); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } //允许请求文件,并且将wwwroot作为默认目录 app.UseFileServer().UseDefaultFiles(); //启用webSocket中间件 app.UseWebSockets(); //拦截请求,针对特定的请求进行处理 app.Use(async(context, next) => { //websocket请求的路径 if (context.Request.Path == "/ws") { if (context.WebSockets.IsWebSocketRequest) { WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); //处理websocket await EchoHandler.Echo(webSocket); } else//如果当前请求不是websocket,则直接返回 { context.Response.StatusCode = 400; } } else { await next(); } }); app.Run(async(context) => { await context.Response.WriteAsync("Hello World!"); }); }