Esempio n. 1
0
        public async Task <Channel <Message> > CreateChannelAsync(
            Symbol publisherId, CancellationToken cancellationToken)
        {
            var clientId = ClientId.Value;

            try {
                var connectionUri = ConnectionUrlResolver.Invoke(this, publisherId);
                _log.LogInformation($"{clientId}: Connecting to {connectionUri}...");
                var ws = ClientWebSocketFactory.Invoke(ServiceProvider);
                using var cts = new CancellationTokenSource(ConnectTimeout);
                using var lts = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, cancellationToken);
                await ws.ConnectAsync(connectionUri, lts.Token).ConfigureAwait(false);

                _log.LogInformation($"{clientId}: Connected.");

                var wsChannel = new WebSocketChannel(ws);
                Channel <string> stringChannel = wsChannel;
                if (MessageLogLevel.HasValue)
                {
                    stringChannel = stringChannel.WithLogger(
                        clientId, _log,
                        MessageLogLevel.GetValueOrDefault(),
                        MessageMaxLength);
                }
                var serializers   = ChannelSerializerPairFactory.Invoke(ServiceProvider);
                var resultChannel = stringChannel.WithSerializers(serializers);
                wsChannel.WhenCompletedAsync(default).ContinueWith(async _ => {
        public async Task <Channel <Message> > CreateChannelAsync(
            Symbol publisherId, CancellationToken cancellationToken)
        {
            var clientId = ClientId.Value;

            try {
                var connectionUri = GetConnectionUrl(publisherId);
                _log.LogInformation($"{clientId}: Connecting to {connectionUri}...");
                var ws = ClientWebSocketFactory.Invoke(ServiceProvider);
                using var cts = new CancellationTokenSource(ConnectTimeout);
                using var lts = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, cancellationToken);
                await ws.ConnectAsync(connectionUri, lts.Token).ConfigureAwait(false);

                _log.LogInformation($"{clientId}: Connected.");

                await using var wsChannel = new WebSocketChannel(ws);
                Channel <string> stringChannel = wsChannel;
                if (MessageLogLevel.HasValue)
                {
                    stringChannel = stringChannel.WithLogger(
                        clientId, _log,
                        MessageLogLevel.GetValueOrDefault(),
                        MessageMaxLength);
                }
                var serializers   = ChannelSerializerPairFactory.Invoke(ServiceProvider);
                var resultChannel = stringChannel.WithSerializers(serializers);
                return(resultChannel);
            }
            catch (OperationCanceledException) {
                if (cancellationToken.IsCancellationRequested)
                {
                    throw;
                }
                throw Errors.WebSocketConnectTimeout();
            }
            catch (Exception e) {
                _log.LogError(e, $"{clientId}: Error.");
                throw;
            }
        }