public async Task Invoke(HttpContext context) { if (!context.WebSockets.IsWebSocketRequest) { return; } var socket = await context.WebSockets.AcceptWebSocketAsync(); _webSocketService.OnConnected(socket); await Receive(socket, async (result, buffer) => { if (result.MessageType == WebSocketMessageType.Text) { await _webSocketService.ReceiveAsync(result, buffer); return; } if (result.MessageType == WebSocketMessageType.Close) { await _webSocketService.OnDisconnected(socket); } }); await _next.Invoke(context); }