/// <summary>
        /// Start discovering.
        /// </summary>
        public async Task StartAsync()
        {
            if (!IsStarted)
            {
                _discoveredServices = new List <ResonanceSignalRDiscoveredService <TReportedServiceInformation> >();

                _client = SignalRClientFactory.Default.Create(Mode, HubUrl);
                _client.EnableAutoReconnection = EnableAutoReconnection;
                _client.Error += OnDisconnected;
                await _client.StartAsync();

                await _client.InvokeAsync(ResonanceHubMethods.RegisterDiscoveryClient, Credentials);

                var services = await _client.InvokeAsync <List <TReportedServiceInformation> >(ResonanceHubMethods.GetAvailableServices, Credentials);

                foreach (var serviceInfo in services)
                {
                    OnServiceRegistered(serviceInfo);
                }

                _client.On <TReportedServiceInformation>(ResonanceHubMethods.ServiceRegistered, OnServiceRegistered);
                _client.On <TReportedServiceInformation>(ResonanceHubMethods.ServiceUnRegistered, OnServiceUnregistered);
                IsStarted = true;
            }
        }
 public async Task StartAsync(CancellationToken cancellationToken)
 {
     if (Connection.State == HubConnectionState.Disconnected)
     {
         await _client.StartAsync(cancellationToken);
     }
 }
        /// <summary>
        /// Gets the collection of available services for the provided credentials.
        /// </summary>
        /// <typeparam name="TCredentials">The type of the credentials.</typeparam>
        /// <typeparam name="TReportedServiceInformation">The type of the reported service information.</typeparam>
        /// <param name="credentials">The credentials.</param>
        /// <param name="url">The URL.</param>
        /// <param name="mode">The mode.</param>
        /// <returns></returns>
        public async Task <List <TReportedServiceInformation> > GetAvailableServicesAsync <TCredentials, TReportedServiceInformation>(TCredentials credentials, String url, SignalRMode mode) where TReportedServiceInformation : IResonanceServiceInformation
        {
            ISignalRClient client = SignalRClientFactory.Default.Create(mode, url);

            await client.StartAsync();

            var services = await client.InvokeAsync <List <TReportedServiceInformation> >(ResonanceHubMethods.GetAvailableServices, credentials);

            await client.DisposeAsync();

            return(services);
        }
        /// <summary>
        /// Registers a new Resonance SignalR service.
        /// </summary>
        /// <typeparam name="TCredentials">The type of the credentials.</typeparam>
        /// <typeparam name="TResonanceServiceInformation">The type of the resonance service information.</typeparam>
        /// <typeparam name="TAdapterInformation">The type of the adapter information.</typeparam>
        /// <param name="credentials">The credentials used to authenticate the service.</param>
        /// <param name="serviceInformation">The service information.</param>
        /// <param name="url">The hub URL.</param>
        /// <param name="mode">The SignalR mode (legacy/core).</param>
        /// <returns></returns>
        public async Task <ResonanceRegisteredService <TCredentials, TResonanceServiceInformation, TAdapterInformation> > RegisterServiceAsync <TCredentials, TResonanceServiceInformation, TAdapterInformation>(TCredentials credentials, TResonanceServiceInformation serviceInformation, String url, SignalRMode mode) where TResonanceServiceInformation : IResonanceServiceInformation
        {
            Logger.LogDebug($"Registering service {{@ServiceInformation}}...", serviceInformation);

            ISignalRClient client = SignalRClientFactory.Default.Create(mode, url);

            client.EnableAutoReconnection = true;

            await client.StartAsync();

            await client.InvokeAsync(ResonanceHubMethods.Login, credentials);

            await client.InvokeAsync(ResonanceHubMethods.RegisterService, serviceInformation);

            return(new ResonanceRegisteredService <TCredentials, TResonanceServiceInformation, TAdapterInformation>(credentials, serviceInformation, mode, client));
        }
Exemple #5
0
        /// <summary>
        /// Called when the adapter is connecting.
        /// </summary>
        /// <returns></returns>
        protected override Task OnConnect()
        {
            bool completed = false;

            TaskCompletionSource <object> completionSource = new TaskCompletionSource <object>();

            Task.Factory.StartNew(() =>
            {
                try
                {
                    _client = SignalRClientFactory.Default.Create(Mode, Url);
                    _client.StartAsync().GetAwaiter().GetResult();

                    if (Role == SignalRAdapterRole.Connect)
                    {
                        _client.On(ResonanceHubMethods.Connected, () =>
                        {
                            try
                            {
                                if (!completed)
                                {
                                    completed = true;
                                    completionSource.SetResult(true);
                                }
                            }
                            catch (Exception ex)
                            {
                                if (!completed)
                                {
                                    Logger.LogError(ex, "Error occurred after successful connection.");
                                    completed = true;
                                    completionSource.SetException(ex);
                                }
                            }
                        });

                        _client.On(ResonanceHubMethods.Declined, () =>
                        {
                            try
                            {
                                if (!completed)
                                {
                                    completed = true;

                                    var ex = new ConnectionDeclinedException();

                                    Logger.LogError(ex, "Error occurred after session created.");
                                    completionSource.SetException(ex);
                                }
                            }
                            catch (Exception ex)
                            {
                                if (!completed)
                                {
                                    Logger.LogError(ex, "Error occurred after session created.");
                                    completed = true;
                                    completionSource.SetException(ex);
                                }
                            }
                        });
                    }

                    _client.On(ResonanceHubMethods.Disconnected, () =>
                    {
                        if (State == ResonanceComponentState.Connected)
                        {
                            //OnDisconnect(false); //Don't know what to do here.. We already have the resonance disconnection message.
                            //Maybe just raise an event..
                        }
                    });

                    _client.On(ResonanceHubMethods.ServiceDown, () =>
                    {
                        OnFailed(new ServiceDownException());
                    });

                    Logger.LogInformation("Authenticating with the remote hub {HubUrl}...", _client.Url);
                    _client.InvokeAsync(ResonanceHubMethods.Login, Credentials).GetAwaiter().GetResult();

                    if (Role == SignalRAdapterRole.Connect)
                    {
                        Logger.LogInformation("Connecting to service {ServiceId}...", ServiceId);
                        SessionId = _client.InvokeAsync <String>(ResonanceHubMethods.Connect, ServiceId).GetAwaiter().GetResult();
                    }
                    else
                    {
                        Logger.LogInformation("Accepting connection {SessionId}...", SessionId);
                        _client.InvokeAsync(ResonanceHubMethods.AcceptConnection, SessionId).GetAwaiter().GetResult();

                        if (!completed)
                        {
                            completed = true;
                            completionSource.SetResult(true);
                        }
                    }

                    _client.On <byte[]>(ResonanceHubMethods.DataAvailable, (data) => { OnDataAvailable(data); });

                    _client.Error        += OnError;
                    _client.Reconnecting += OnReconnecting;
                    _client.Reconnected  += OnReconnected;
                }
                catch (Exception ex)
                {
                    completed = true;
                    Logger.LogError(ex, "Error occurred while trying to connect.");
                    completionSource.SetException(ex);
                }
            });

            TimeoutTask.StartNew(() =>
            {
                if (!completed)
                {
                    completed = true;
                    completionSource.SetException(new TimeoutException("Could not connect after the given timeout."));
                }
            }, ConnectionTimeout);

            return(completionSource.Task);
        }