コード例 #1
0
        public TestClient(bool synchronousCallbacks = false, IHubProtocol protocol = null, IInvocationBinder invocationBinder = null, bool addClaimId = false)
        {
            var options = new PipeOptions(readerScheduler: synchronousCallbacks ? PipeScheduler.Inline : null);
            var pair    = DuplexPipe.CreateConnectionPair(options, options);

            Connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Transport, pair.Application);

            var claimValue = Interlocked.Increment(ref _id).ToString();
            var claims     = new List <Claim> {
                new Claim(ClaimTypes.Name, claimValue)
            };

            if (addClaimId)
            {
                claims.Add(new Claim(ClaimTypes.NameIdentifier, claimValue));
            }

            Connection.User = new ClaimsPrincipal(new ClaimsIdentity(claims));
            Connection.Items["ConnectedTask"] = new TaskCompletionSource <bool>();

            _protocol         = protocol ?? new JsonHubProtocol();
            _invocationBinder = invocationBinder ?? new DefaultInvocationBinder();

            _cts = new CancellationTokenSource();

            using (var memoryStream = new MemoryStream())
            {
                NegotiationProtocol.WriteMessage(new NegotiationMessage(_protocol.Name), memoryStream);
                Connection.Application.Output.WriteAsync(memoryStream.ToArray());
            }
        }
コード例 #2
0
        public TestClient(bool synchronousCallbacks = false)
        {
            var options = new ChannelOptimizations {
                AllowSynchronousContinuations = synchronousCallbacks
            };
            var transportToApplication = Channel.CreateUnbounded <byte[]>(options);
            var applicationToTransport = Channel.CreateUnbounded <byte[]>(options);

            Application = ChannelConnection.Create <byte[]>(input: applicationToTransport, output: transportToApplication);
            _transport  = ChannelConnection.Create <byte[]>(input: transportToApplication, output: applicationToTransport);

            Connection      = new DefaultConnectionContext(Guid.NewGuid().ToString(), _transport, Application);
            Connection.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, Interlocked.Increment(ref _id).ToString()) }));
            Connection.Metadata["ConnectedTask"] = new TaskCompletionSource <bool>();

            var protocol = new JsonHubProtocol(new JsonSerializer());

            _protocolReaderWriter = new HubProtocolReaderWriter(protocol, new PassThroughEncoder());

            _cts = new CancellationTokenSource();

            using (var memoryStream = new MemoryStream())
            {
                NegotiationProtocol.WriteMessage(new NegotiationMessage(protocol.Name), memoryStream);
                Application.Out.TryWrite(memoryStream.ToArray());
            }
        }
コード例 #3
0
        private async Task StartAsyncCore()
        {
            var transferModeFeature = _connection.Features.Get <ITransferModeFeature>();

            if (transferModeFeature == null)
            {
                transferModeFeature = new TransferModeFeature();
                _connection.Features.Set(transferModeFeature);
            }

            var requestedTransferMode =
                _protocol.Type == ProtocolType.Binary
                    ? TransferMode.Binary
                    : TransferMode.Text;

            transferModeFeature.TransferMode = requestedTransferMode;
            await _connection.StartAsync();

            var actualTransferMode = transferModeFeature.TransferMode;

            _protocolReaderWriter = new HubProtocolReaderWriter(_protocol, GetDataEncoder(requestedTransferMode, actualTransferMode));

            _logger.HubProtocol(_protocol.Name);

            using (var memoryStream = new MemoryStream())
            {
                NegotiationProtocol.WriteMessage(new NegotiationMessage(_protocol.Name), memoryStream);
                await _connection.SendAsync(memoryStream.ToArray(), _connectionActive.Token);
            }
        }
コード例 #4
0
 private async Task NegotiateProtocol()
 {
     _logger.LogDebug($"Negotiate to use hub protocol: {_protocol.Name}");
     using (var memoryStream = new MemoryStream())
     {
         NegotiationProtocol.WriteMessage(new NegotiationMessage(_protocol.Name), memoryStream);
         await _connection.SendAsync(memoryStream.ToArray(), CancellationToken.None);
     }
 }
コード例 #5
0
        public void CanRoundtripNegotiation()
        {
            var negotiationMessage = new NegotiationMessage(protocol: "dummy");

            using (var ms = new MemoryStream())
            {
                NegotiationProtocol.WriteMessage(negotiationMessage, ms);
                Assert.True(NegotiationProtocol.TryParseMessage(ms.ToArray(), out var deserializedMessage));

                Assert.NotNull(deserializedMessage);
                Assert.Equal(negotiationMessage.Protocol, deserializedMessage.Protocol);
            }
        }
コード例 #6
0
        private async Task StartAsyncCore()
        {
            await _connection.StartAsync(_protocol.TransferFormat);

            _needKeepAlive = _connection.Features.Get <IConnectionInherentKeepAliveFeature>() == null;

            Log.HubProtocol(_logger, _protocol.Name);

            _connectionActive = new CancellationTokenSource();
            using (var memoryStream = new MemoryStream())
            {
                Log.SendingHubNegotiate(_logger);
                NegotiationProtocol.WriteMessage(new NegotiationMessage(_protocol.Name), memoryStream);
                await _connection.SendAsync(memoryStream.ToArray(), _connectionActive.Token);
            }

            ResetTimeoutTimer();
        }
コード例 #7
0
ファイル: TestClient.cs プロジェクト: xscript/SignalR
        public TestClient(bool synchronousCallbacks = false, IHubProtocol protocol = null, IInvocationBinder invocationBinder = null, bool addClaimId = false)
        {
            var options = new UnboundedChannelOptions {
                AllowSynchronousContinuations = synchronousCallbacks
            };
            var transportToApplication = Channel.CreateUnbounded <byte[]>(options);
            var applicationToTransport = Channel.CreateUnbounded <byte[]>(options);

            Application = ChannelConnection.Create <byte[]>(input: applicationToTransport, output: transportToApplication);
            _transport  = ChannelConnection.Create <byte[]>(input: transportToApplication, output: applicationToTransport);

            Connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), _transport, Application);

            var claimValue = Interlocked.Increment(ref _id).ToString();
            var claims     = new List <Claim> {
                new Claim(ClaimTypes.Name, claimValue)
            };

            if (addClaimId)
            {
                claims.Add(new Claim(ClaimTypes.NameIdentifier, claimValue));
            }

            Connection.User = new ClaimsPrincipal(new ClaimsIdentity(claims));
            Connection.Metadata["ConnectedTask"] = new TaskCompletionSource <bool>();

            protocol = protocol ?? new JsonHubProtocol();
            _protocolReaderWriter = new HubProtocolReaderWriter(protocol, new PassThroughEncoder());
            _invocationBinder     = invocationBinder ?? new DefaultInvocationBinder();

            _cts = new CancellationTokenSource();

            using (var memoryStream = new MemoryStream())
            {
                NegotiationProtocol.WriteMessage(new NegotiationMessage(protocol.Name), memoryStream);
                Application.Writer.TryWrite(memoryStream.ToArray());
            }
        }
コード例 #8
0
ファイル: HubConnection.cs プロジェクト: pardahlman/SignalR
        private async Task StartAsyncCore()
        {
            var transferModeFeature = _connection.Features.Get <ITransferModeFeature>();

            if (transferModeFeature == null)
            {
                transferModeFeature = new TransferModeFeature();
                _connection.Features.Set(transferModeFeature);
            }

            var requestedTransferMode =
                _protocol.Type == ProtocolType.Binary
                    ? TransferMode.Binary
                    : TransferMode.Text;

            transferModeFeature.TransferMode = requestedTransferMode;
            await _connection.StartAsync();

            _needKeepAlive = _connection.Features.Get <IConnectionInherentKeepAliveFeature>() == null;

            var actualTransferMode = transferModeFeature.TransferMode;

            _protocolReaderWriter = new HubProtocolReaderWriter(_protocol, GetDataEncoder(requestedTransferMode, actualTransferMode));

            Log.HubProtocol(_logger, _protocol.Name);

            _connectionActive = new CancellationTokenSource();
            using (var memoryStream = new MemoryStream())
            {
                Log.SendingHubNegotiate(_logger);
                NegotiationProtocol.WriteMessage(new NegotiationMessage(_protocol.Name), memoryStream);
                await _connection.SendAsync(memoryStream.ToArray(), _connectionActive.Token);
            }

            ResetTimeoutTimer();
        }