public static IApplicationBuilder UseProxyWebSockets(this IApplicationBuilder app, CancellationToken cancellationToken = default(CancellationToken))
        {
            ThrowIfServiceNotRegistered(app.ApplicationServices);
            var appLifeTime = app.ApplicationServices.GetService <IApplicationLifetime>();
            IList <IWebSocketConnector> connectors = InvocatorFactory.GetConnectors(app.ApplicationServices);

            foreach (var connector in connectors)
            {
                InvocatorsHelper.EnsureHostPair(connector.InvocatorContext);
                appLifeTime.ApplicationStopping.Register(OnShutdown, connector);
                Task.Factory.StartNew(async() => await connector.ConnectAsync(cancellationToken), TaskCreationOptions.LongRunning);
            }

            return(app);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TInvocator"></typeparam>
        /// <param name="connectorName"></param>
        /// <param name="hostAddress">Unique host address</param>
        /// <returns></returns>
        public ProxyWebSocketsBuilder Register <TInvocator>(string connectorName, string hostAddress)
            where TInvocator : IClientWebSocketCommandInvocator
        {
            var invocatorType = typeof(TInvocator);

            InvocatorsHelper.EnsureHostPair(invocatorType, connectorName, hostAddress);

            RegisterInternal <TInvocator>();

            var proxyOptions = new ProxyOptions <TInvocator>
            {
                ConnectorName        = connectorName,
                WebSocketHostAddress = hostAddress
            };

            _services.AddSingleton(Options.Create(proxyOptions));
            _services.AddSingleton <IClientInvocatorContextFactory <TInvocator>, DefaultClientInvocatorContextFactory <TInvocator> >();
            return(this);
        }