Beispiel #1
0
        private async Task HandleAfterSocketAsync
            (WebSocket nativeSocket, MariBaseWebSocketService service, HttpContext context)
        {
            using var socket = new MariWebSocket(nativeSocket, service.Cts.Token, service);
            _logger.LogDebug($"The new WebSocket has the Id: {socket.Id}");

            service.AddClient(socket);

            await service.OnOpenAsync(socket, context)
            .Try(_logger, service, socket, false);

            try
            {
                await ReadAsync(socket, service, context);
            }
            catch (Exception ex)
            {
                if (ex is TaskCanceledException)
                {
                    return;
                }

                await service.OnDisconnectedAsync(socket, WebSocketCloseStatus.ProtocolError, ex.Message)
                .Try(_logger, service, socket, false);

                if (socket.WebSocket.State.Equals(WebSocketState.Open))
                {
                    await socket.WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure,
                                                      "Closed by remote", service.Cts.Token)
                    .Try(_logger, service, socket, false);
                }
            }
            nativeSocket.Dispose();
        }