Ejemplo n.º 1
0
        private async Task <ClientWebSocketReceiver> TryConnectAsync(CancellationToken cancellationToken)
        {
            _webSocket = new ClientWebSocket();
            _webSocket.Options.SetRequestHeader(NCSConstants.ConnectorName, InvocatorContext.ConnectorName);
            try
            {
                await _webSocket.ConnectAsync(InvocatorContext.Uri, cancellationToken);
            }
            catch (Exception ex)
            {
                ProxyLogHelper.Log(_loggerFactory, InvocatorContext, "Error", ex);
                return(null);
            }

            var receiverContext = new ClientWebSocketReceiverContext
            {
                Compressor       = _compressor,
                InvocatorContext = InvocatorContext,
                LoggerFactory    = _loggerFactory,
                WebSocket        = _webSocket
            };

            var receiver = new ClientWebSocketReceiver(_serviceProvider, receiverContext, Close, (connectionId) => {
                _connectionId = connectionId;
            });

            return(receiver);
        }
Ejemplo n.º 2
0
        private async Task TryConnectAsync(CancellationTokenSource cancellationTokenSource = null)
        {
            var invocatorContext = CreateInvocatorContext();
            var uri = new Uri($"ws://{invocatorContext.HostAddress}");

            _webSocket = new ClientWebSocket();
            _webSocket.Options.SetRequestHeader(SocketsConstants.ConnectorName, invocatorContext.ConnectorName);
            try
            {
                CancellationToken token = cancellationTokenSource != null ? cancellationTokenSource.Token : CancellationToken.None;
                await _webSocket.ConnectAsync(uri, token);
            }
            catch (Exception ex)
            {
                ProxyLogHelper.Log(_loggerFactory, invocatorContext, "Error", ex);
                return;
            }

            var receiverContext = new WebSocketReceiverContext
            {
                Compressor       = _compressor,
                InvocatorContext = invocatorContext,
                LoggerFactory    = _loggerFactory,
                WebSocket        = _webSocket
            };
            var receiver = new WebSocketReceiver(_serviceProvider, receiverContext, Close, (connectionId) => {
                _connectionId = connectionId;
            });
            await receiver.ReceiveAsync();
        }