Beispiel #1
0
        private async Task HandleRequests(HttpContext context)
        {
            var webSocket = await context.WebSockets.AcceptWebSocketAsync();

            if (context.Request.Path.Value == "/messages/send")
            {
                WebSocketReceiveResult result = await _messageReceiver.ReceiveMessages(webSocket);

                var status = result.CloseStatus ?? WebSocketCloseStatus.Empty;

                await webSocket.CloseAsync(
                    status,
                    result.CloseStatusDescription,
                    default(CancellationToken)
                    );
            }
            else if (context.Request.Path.Value == "/messages/receive")
            {
                await _messageSender.SendUserRelatedMessages(webSocket);

                await webSocket.CloseAsync(
                    WebSocketCloseStatus.Empty,
                    "closed successfully",
                    default(CancellationToken)
                    );
            }
            else // "/events"
            {
                await _eventSender.SendUserRelatedEvents(webSocket);

                await webSocket.CloseAsync(
                    WebSocketCloseStatus.Empty,
                    "success",
                    default(CancellationToken)
                    );
            }
        }