Beispiel #1
0
        public async Task WebSocketRequestHandler(HttpContext context)
        {
            var authorizationInfo = await _authService.Authenticate(context.Request).ConfigureAwait(false);

            if (!authorizationInfo.IsAuthenticated)
            {
                throw new SecurityException("Token is required");
            }

            try
            {
                _logger.LogInformation("WS {IP} request", context.Connection.RemoteIpAddress);

                WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false);

                using var connection = new WebSocketConnection(
                          _loggerFactory.CreateLogger <WebSocketConnection>(),
                          webSocket,
                          context.Connection.RemoteIpAddress,
                          context.Request.Query)
                      {
                          OnReceive = ProcessWebSocketMessageReceived
                      };

                var tasks = new Task[_webSocketListeners.Length];
                for (var i = 0; i < _webSocketListeners.Length; ++i)
                {
                    tasks[i] = _webSocketListeners[i].ProcessWebSocketConnectedAsync(connection);
                }

                await Task.WhenAll(tasks).ConfigureAwait(false);

                await connection.ProcessAsync().ConfigureAwait(false);

                _logger.LogInformation("WS {IP} closed", context.Connection.RemoteIpAddress);
            }
            catch (Exception ex) // Otherwise ASP.Net will ignore the exception
            {
                _logger.LogError(ex, "WS {IP} WebSocketRequestHandler error", context.Connection.RemoteIpAddress);
                if (!context.Response.HasStarted)
                {
                    context.Response.StatusCode = 500;
                }
            }
        }
Beispiel #2
0
        private async Task WebSocketRequestHandler(HttpContext context)
        {
            if (_disposed)
            {
                return;
            }

            try
            {
                _logger.LogInformation("WS {IP} request", context.Connection.RemoteIpAddress);

                WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false);

                var connection = new WebSocketConnection(
                    _loggerFactory.CreateLogger <WebSocketConnection>(),
                    webSocket,
                    context.Connection.RemoteIpAddress,
                    context.Request.Query)
                {
                    OnReceive = ProcessWebSocketMessageReceived
                };

                WebSocketConnected?.Invoke(this, new GenericEventArgs <IWebSocketConnection>(connection));

                await connection.ProcessAsync().ConfigureAwait(false);

                _logger.LogInformation("WS {IP} closed", context.Connection.RemoteIpAddress);
            }
            catch (Exception ex) // Otherwise ASP.Net will ignore the exception
            {
                _logger.LogError(ex, "WS {IP} WebSocketRequestHandler error", context.Connection.RemoteIpAddress);
                if (!context.Response.HasStarted)
                {
                    context.Response.StatusCode = 500;
                }
            }
        }