public async Task Invoke(HttpContext context, ICustomWebSocketFactory wsFactory, ICustomWebSocketMessageHandler wsmHandler) { if (context.Request.Path == "/ws") { if (context.Request.Headers.ContainsKey("Sec-WebSocket-Protocol")) { context.Response.Headers.Add("Sec-WebSocket-Protocol", "protocol1"); } if (context.WebSockets.IsWebSocketRequest) { string username = context.Request.Query["u"]; string token = context.Request.Query["token"]; //Log4netHelper.Info(this, "websokct进入,用户为:" + username); if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(token) && token.Length == 36) { WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); CustomWebSocket userWebSocket = new CustomWebSocket() { WebSocket = webSocket, Username = username, token = token }; wsFactory.Add(userWebSocket); wsFactory.RemoveInvalid(); await wsmHandler.SendInitialMessages(userWebSocket); await Listen(context, userWebSocket, wsFactory, wsmHandler); } } else { context.Response.StatusCode = 400; //Log4netHelper.Info(this, "不是websocket请求!"); } } else { await _next(context); } }