/// <summary> /// 启用 WebSocket 服务器 并支持对象注入 /// </summary> /// <param name="app"></param> /// <param name="options">WebSocket选项</param> /// <param name="injectionDic">注入对象字典</param> /// <returns></returns> public static IApplicationBuilder EnabledWebSocket(this IApplicationBuilder app, WebSocketOptions options) { WebSocketControllerManager.InitContext(app); if (options != null) { app.UseWebSockets(options); } else { app.UseWebSockets(); } app.Use(WebSocketControllerManager.PipelineHandle); return(app); }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <returns></returns> internal static async Task PipelineHandle(HttpContext context, Func <Task> next) { if (context.WebSockets.IsWebSocketRequest) { var controller = WebSocketControllerManager.MatchingController(context.Request.Path); if (controller != null) { await controller.AcceptWebSocketAsync(context); } else { context.Response.StatusCode = 400; } } else { await next(); } }