public void GlobalSetup()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSignalRCore();

            var provider = serviceCollection.BuildServiceProvider();

            var serviceScopeFactory = provider.GetService <IServiceScopeFactory>();

            _dispatcher = new DefaultHubDispatcher <TestHub>(
                serviceScopeFactory,
                new HubContext <TestHub>(new DefaultHubLifetimeManager <TestHub>(NullLogger <DefaultHubLifetimeManager <TestHub> > .Instance)),
                enableDetailedErrors: false,
                new Logger <DefaultHubDispatcher <TestHub> >(NullLoggerFactory.Instance));

            var pair       = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
            var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Application, pair.Transport);

            var contextOptions = new HubConnectionContextOptions()
            {
                KeepAliveInterval = TimeSpan.Zero,
            };

            _connectionContext = new NoErrorHubConnectionContext(connection, contextOptions, NullLoggerFactory.Instance);

            _connectionContext.Protocol = new FakeHubProtocol();
        }
        public void GlobalSetup()
        {
            var memoryBufferWriter = MemoryBufferWriter.Get();

            try
            {
                HandshakeProtocol.WriteRequestMessage(new HandshakeRequestMessage("json", 1), memoryBufferWriter);
                _handshakeRequestResult = new ReadResult(new ReadOnlySequence <byte>(memoryBufferWriter.ToArray()), false, false);
            }
            finally
            {
                MemoryBufferWriter.Return(memoryBufferWriter);
            }

            _pipe = new TestDuplexPipe();

            var connection     = new DefaultConnectionContext(Guid.NewGuid().ToString(), _pipe, _pipe);
            var contextOptions = new HubConnectionContextOptions()
            {
                KeepAliveInterval = Timeout.InfiniteTimeSpan,
            };

            _hubConnectionContext = new HubConnectionContext(connection, contextOptions, NullLoggerFactory.Instance);

            _successHubProtocolResolver = new TestHubProtocolResolver(new NewtonsoftJsonHubProtocol());
            _failureHubProtocolResolver = new TestHubProtocolResolver(null);
            _userIdProvider             = new TestUserIdProvider();
            _supportedProtocols         = new List <string> {
                "json"
            };
        }
Exemplo n.º 3
0
        public void GlobalSetup()
        {
            _hubLifetimeManager = new DefaultHubLifetimeManager <Hub>(NullLogger <DefaultHubLifetimeManager <Hub> > .Instance);

            IHubProtocol protocol;

            if (Protocol == "json")
            {
                protocol = new NewtonsoftJsonHubProtocol();
            }
            else
            {
                protocol = new MessagePackHubProtocol();
            }

            var options = new PipeOptions();

            for (var i = 0; i < Connections; ++i)
            {
                var pair           = DuplexPipe.CreateConnectionPair(options, options);
                var connection     = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Application, pair.Transport);
                var contextOptions = new HubConnectionContextOptions()
                {
                    KeepAliveInterval = Timeout.InfiniteTimeSpan,
                };
                var hubConnection = new HubConnectionContext(connection, contextOptions, NullLoggerFactory.Instance);
                hubConnection.Protocol = protocol;
                _hubLifetimeManager.OnConnectedAsync(hubConnection).GetAwaiter().GetResult();
                _hubLifetimeManager.AddToGroupAsync(connection.ConnectionId, TestGroupName).GetAwaiter().GetResult();

                _ = ConsumeAsync(connection.Application);
            }

            _hubContext = new HubContext <Hub>(_hubLifetimeManager);
        }
Exemplo n.º 4
0
        public static MockHubConnectionContext CreateMock(ConnectionContext connection)
        {
            var contextOptions = new HubConnectionContextOptions()
            {
                KeepAliveInterval     = TimeSpan.FromSeconds(15),
                ClientTimeoutInterval = TimeSpan.FromSeconds(15),
                StreamBufferCapacity  = 10,
            };

            return(new MockHubConnectionContext(connection, contextOptions, NullLoggerFactory.Instance));
        }
Exemplo n.º 5
0
    /// <summary>
    /// Initializes a new instance of the <see cref="HubConnectionContext"/> class.
    /// </summary>
    /// <param name="connectionContext">The underlying <see cref="ConnectionContext"/>.</param>
    /// <param name="loggerFactory">The logger factory.</param>
    /// <param name="contextOptions">The options to configure the HubConnectionContext.</param>
    public HubConnectionContext(ConnectionContext connectionContext, HubConnectionContextOptions contextOptions, ILoggerFactory loggerFactory)
    {
        _keepAliveInterval     = (long)contextOptions.KeepAliveInterval.TotalMilliseconds;
        _clientTimeoutInterval = (long)contextOptions.ClientTimeoutInterval.TotalMilliseconds;
        _streamBufferCapacity  = contextOptions.StreamBufferCapacity;
        _maxMessageSize        = contextOptions.MaximumReceiveMessageSize;

        _connectionContext  = connectionContext;
        _logger             = loggerFactory.CreateLogger <HubConnectionContext>();
        ConnectionAborted   = _connectionAbortedTokenSource.Token;
        _closedRegistration = connectionContext.ConnectionClosed.Register(static (state) => ((HubConnectionContext)state !).Abort(), this);
Exemplo n.º 6
0
        public static HubConnectionContext Create(ConnectionContext connection, IHubProtocol protocol = null, string userIdentifier = null)
        {
            var contextOptions = new HubConnectionContextOptions()
            {
                KeepAliveInterval = TimeSpan.FromSeconds(15),
            };

            return(new HubConnectionContext(connection, contextOptions, NullLoggerFactory.Instance)
            {
                Protocol = protocol ?? new JsonHubProtocol(),
                UserIdentifier = userIdentifier,
            });
        }
Exemplo n.º 7
0
    /// <inheritdoc />
    public override async Task OnConnectedAsync(ConnectionContext connection)
    {
        // We check to see if HubOptions<THub> are set because those take precedence over global hub options.
        // Then set the keepAlive and handshakeTimeout values to the defaults in HubOptionsSetup when they were explicitly set to null.

        var supportedProtocols = _hubOptions.SupportedProtocols ?? _globalHubOptions.SupportedProtocols;

        if (supportedProtocols == null || supportedProtocols.Count == 0)
        {
            throw new InvalidOperationException("There are no supported protocols");
        }

        var handshakeTimeout = _hubOptions.HandshakeTimeout ?? _globalHubOptions.HandshakeTimeout ?? HubOptionsSetup.DefaultHandshakeTimeout;

        var contextOptions = new HubConnectionContextOptions()
        {
            KeepAliveInterval         = _hubOptions.KeepAliveInterval ?? _globalHubOptions.KeepAliveInterval ?? HubOptionsSetup.DefaultKeepAliveInterval,
            ClientTimeoutInterval     = _hubOptions.ClientTimeoutInterval ?? _globalHubOptions.ClientTimeoutInterval ?? HubOptionsSetup.DefaultClientTimeoutInterval,
            StreamBufferCapacity      = _hubOptions.StreamBufferCapacity ?? _globalHubOptions.StreamBufferCapacity ?? HubOptionsSetup.DefaultStreamBufferCapacity,
            MaximumReceiveMessageSize = _maximumMessageSize,
            SystemClock = SystemClock,
            MaximumParallelInvocations = _maxParallelInvokes,
        };

        Log.ConnectedStarting(_logger);

        var connectionContext = new HubConnectionContext(connection, contextOptions, _loggerFactory);

        var resolvedSupportedProtocols = (supportedProtocols as IReadOnlyList <string>) ?? supportedProtocols.ToList();

        if (!await connectionContext.HandshakeAsync(handshakeTimeout, resolvedSupportedProtocols, _protocolResolver, _userIdProvider, _enableDetailedErrors))
        {
            return;
        }

        // -- the connectionContext has been set up --

        try
        {
            await _lifetimeManager.OnConnectedAsync(connectionContext);
            await RunHubAsync(connectionContext);
        }
        finally
        {
            connectionContext.Cleanup();

            Log.ConnectedEnding(_logger);
            await _lifetimeManager.OnDisconnectedAsync(connectionContext);
        }
    }
Exemplo n.º 8
0
    public void GlobalSetup()
    {
        _hubLifetimeManager  = new DefaultHubLifetimeManager <Hub>(NullLogger <DefaultHubLifetimeManager <Hub> > .Instance);
        _connectionIds       = new List <string>();
        _subsetConnectionIds = new List <string>();
        _groupNames          = new List <string>();
        _userIdentifiers     = new List <string>();

        var jsonHubProtocol = new NewtonsoftJsonHubProtocol();

        for (int i = 0; i < 100; i++)
        {
            string connectionId   = "connection-" + i;
            string groupName      = "group-" + i % 10;
            string userIdentifier = "user-" + i % 20;
            AddUnique(_connectionIds, connectionId);
            AddUnique(_groupNames, groupName);
            AddUnique(_userIdentifiers, userIdentifier);
            if (i % 3 == 0)
            {
                _subsetConnectionIds.Add(connectionId);
            }

            var connectionContext = new TestConnectionContext
            {
                ConnectionId = connectionId,
                Transport    = new TestDuplexPipe(ForceAsync)
            };
            var contextOptions = new HubConnectionContextOptions()
            {
                KeepAliveInterval = TimeSpan.Zero,
            };
            var hubConnectionContext = new HubConnectionContext(connectionContext, contextOptions, NullLoggerFactory.Instance);
            hubConnectionContext.UserIdentifier = userIdentifier;
            hubConnectionContext.Protocol       = jsonHubProtocol;

            _hubLifetimeManager.OnConnectedAsync(hubConnectionContext).GetAwaiter().GetResult();
            _hubLifetimeManager.AddToGroupAsync(connectionId, groupName);
        }
    }
        public EmulatorHubConnectionContext(string hub, ConnectionContext connectionContext, HubConnectionContextOptions contextOptions, ILoggerFactory loggerFactory) : base(connectionContext, contextOptions, loggerFactory)
        {
            ConnectionContext = connectionContext ?? throw new ArgumentNullException(nameof(connectionContext));
            var upstreamProperties = new HttpUpstreamPropertiesFeature(connectionContext, hub);

            Features.Set <IHttpUpstreamPropertiesFeature>(upstreamProperties);
        }
 public NoErrorHubConnectionContext(ConnectionContext connectionContext, HubConnectionContextOptions contextOptions, ILoggerFactory loggerFactory)
     : base(connectionContext, contextOptions, loggerFactory)
 {
 }