public async Task OnReceiveAsync(IWebSocketConnection webSocketConnection)
        {
            if (webSocketConnection.WebSocket.State != WebSocketState.Open)
            {
                return;
            }
            _logger.LogInformation("Receive message on websocket connection {0}", webSocketConnection.ConnectionId.ToString());
            var buffer = WebSocket.CreateServerBuffer(BufferSize);
            WebSocketReceiveResult result;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                do
                {
                    result = await webSocketConnection.WebSocket.ReceiveAsync(buffer, CancellationToken.None);

                    if (result.Count > 0)
                    {
                        memoryStream.Write(buffer.Array, buffer.Offset, result.Count);
                    }
                } while (!result.EndOfMessage && result.CloseStatus == null);

                memoryStream.Position = 0;
                using (var streamReader = new StreamReader(memoryStream, Encoding.UTF8))
                {
                    var message = streamReader.ReadToEnd();
                    var conv    = new InternalMessageJsonDeserializer();
                    try
                    {
                        await _mediator.ReceiveAsync(webSocketConnection, conv.Deserialize(message));
                    }
                    catch (ArgumentException ex)
                    {
                        _logger.LogError(ex, ex.Message);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex.Message);
                        _logger.LogError(ex.StackTrace);
                        _logger.LogError("Message = {0}", message);
                        await _internalMessageSender.SendAsync(webSocketConnection, new WebSocketMessageContract()
                        {
                            Type = WebSocketMessageResponseTypes.ERROR,
                            Data = new Error()
                            {
                                Description = "Fatal error occured - " + ex.Message,
                                Exception   = ex
                            }
                        });
                    }
                }
            }
        }
Пример #2
0
        public async Task ReceiveAsync(IWebSocketConnection webSocketConnection, WebSocketMessageContract message)
        {
            var connectionId = webSocketConnection.ConnectionId;

            if (message == null)
            {
                throw new ArgumentNullException("Message cannot be null");
            }

            _logger.LogInformation("Receive message {0}", message.Type);

            switch (message.Type)
            {
            case WebSocketMessageRequestTypes.CONFIG:
                ReceiveConfigMessage((ConfigMessage)message.Data, connectionId);
                break;

            case WebSocketMessageRequestTypes.MESSAGE:
                var clientInternalMessage = (ClientInternalMessage)message.Data;
                try
                {
                    await ReceiveInternalMessageAsync(webSocketConnection, clientInternalMessage);
                }
                catch (Exception ex)
                {
                    await _internalMessageSender.SendAsync(
                        new WebSocketConnection(webSocketConnection.WebSocket, connectionId),
                        new WebSocketMessageContract()
                    {
                        Type = WebSocketMessageResponseTypes.INTERNAL_MESSAGE_ERROR,
                        Data = new InternalMessageError()
                        {
                            CorrelationId = clientInternalMessage.CorrelationId.Value,
                            Description   = "Error occured: " + ex.Message
                        }
                    });
                }
                break;

            case WebSocketMessageRequestTypes.SYNCHRONIZATION:
                var clientSynchronizationMessage = (ClientSynchronizationMessage)message.Data;
                await ReceiveSynchronizationMessage(webSocketConnection, clientSynchronizationMessage);

                break;

            default:
                throw new ArgumentException(nameof(message.Type));
            }
        }