示例#1
0
        public async Task WebSocketRequestHandler(HttpContext context)
        {
            _ = await _authService.Authenticate(context.Request).ConfigureAwait(false);

            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;
                }
            }
        }
示例#2
0
        private void OnWebSocketConnected(WebSocketConnectEventArgs e)
        {
            if (_disposed)
            {
                return;
            }

            var connection = new WebSocketConnection(e.WebSocket, e.Endpoint, _jsonSerializer, _logger, _textEncoding)
            {
                OnReceive   = ProcessWebSocketMessageReceived,
                Url         = e.Url,
                QueryString = e.QueryString ?? new QueryParamCollection()
            };

            connection.Closed += Connection_Closed;

            lock (_webSocketConnections)
            {
                _webSocketConnections.Add(connection);
            }

            WebSocketConnected?.Invoke(this, new GenericEventArgs <IWebSocketConnection>(connection));
        }
示例#3
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;
                }
            }
        }