Example #1
0
        public static IObservable <FSharpOption <HubConnection> > Observe(
            this IHubConnectionBuilder hubConnectionBuilder
            )
        {
            return(Observable.Create <FSharpOption <HubConnection> >(
                       async(observer, cancellationToken) =>
            {
                HubConnection hubConnection = default;

                try
                {
                    observer.OnNext(FSharpOption <HubConnection> .None);

                    hubConnection = hubConnectionBuilder.Build();

                    hubConnection.Closed += exception =>
                    {
                        if (exception is Exception e)
                        {
                            observer.OnError(e);
                        }
                        else
                        {
                            observer.OnCompleted();
                        }

                        return Task.CompletedTask;
                    };

                    await hubConnection.StartAsync(cancellationToken);

                    observer.OnNext(FSharpOption <HubConnection> .Some(hubConnection));
                }
                catch (Exception e)
                {
                    try
                    {
                        await hubConnection?.DisposeAsync();
                    }
                    catch
                    {
                    }

                    observer.OnError(e);
                }

                return () =>
                {
                    try
                    {
                        hubConnection?.DisposeAsync();
                    }
                    catch
                    {
                    }
                };
            }
                       ));
        }
 public MainPageViewModel(IHubConnectionBuilder builder, IDispatcher dispatcher, IAlertMessageService alertService)
 {
     SendMessage  = new DelegateCommand <string>(DoSendMessage);
     Connect      = new DelegateCommand(DoConnect);
     Disconnect   = new DelegateCommand(DoDisconnect);
     Connection   = builder.Build();
     Dispatcher   = dispatcher;
     AlertService = alertService;
 }
Example #3
0
 HubConnection DefaultConnectionBuilder(string route, IHubConnectionBuilder builder)
 {
     builder = builder.WithUrl(route, ConfigureOptions);
     if (AutoReconnect)
     {
         builder.WithAutomaticReconnect();
     }
     if (ConfigureLogging != null)
     {
         builder.ConfigureLogging(ConfigureLogging);
     }
     return(builder.Build());
 }
Example #4
0
        public SignalRService(
            IHubConnectionBuilder hubConnectionBuilder,
            PinService pinService,
            QueuePollerService queuePollerService,
            ILogger <SignalRService> logger)
        {
            hubConnectionBuilder.WithUrl("http://localhost/pinponhub");

            connection              = hubConnectionBuilder.Build();
            this.pinService         = pinService;
            this.queuePollerService = queuePollerService;
            this.logger             = logger;
        }
 public SynthbotSignalrClient(
     ILogger <SynthbotSignalrClient> logger,
     IHubConnectionBuilder hubConnectionBuilder,
     SpotifyWebAPI spotifyApi,
     IConfiguration config,
     DiscordSocketClient discordClient)
 {
     _config        = config;
     _logger        = logger;
     _connection    = hubConnectionBuilder.Build();
     _spotifyApi    = spotifyApi;
     _discordClient = discordClient;
     RegisterHandlers();
 }
Example #6
0
        private async Task EnsureConnected(CancellationToken cancellationToken)
        {
            CheckDisposed();

            if (_hubConnection == null)
            {
                _hubConnection               = _hubConnectionBuilder.Build();
                _hubConnection.Closed       += HubConnectionOnClosed;
                _hubConnection.Reconnecting += HubConnectionOnReconnecting;
                _hubConnection.Reconnected  += HubConnectionOnReconnected;

                _hubConnection.On <IList <Message> >("Notify", OnNotify);
            }

            while (_hubConnection.State == HubConnectionState.Reconnecting)
            {
                cancellationToken.ThrowIfCancellationRequested();
                Task <bool> reconnectingTask = null;
                lock (_reconnectingLock)
                {
                    reconnectingTask = _reconnectingTask?.Task;
                }

                if (reconnectingTask != null)
                {
                    await reconnectingTask;
                }
                else
                {
                    await Task.Yield();
                }
            }

            if (_hubConnection.State == HubConnectionState.Disconnected)
            {
                await _hubConnection.StartAsync(cancellationToken);
            }
        }
Example #7
0
        /// <summary>
        /// Attempts to connect to the hub.
        /// </summary>
        public async Task Connect()
        {
            _logger.LogInformation("Attempting to connect to the message bus.");

            if (String.IsNullOrEmpty(controllerSettings.JwtToken))
            {
                _logger.LogInformation("JWT token is missing or not found. Cannot connect.");
                return;
            }

            try
            {
                // First check our controller's state via the API endpoint.
                _logger.LogInformation("Checking client enrollment state via CCS API.");
                HttpResponseMessage message = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, serverUrl + "/api/Enrollment/Get/" + controllerSettings.Guid.ToString()));

                // If the controller object isn't found, the controller has been unenrolled
                // without being notified. Controller should force disconnect itself.
                if (message.StatusCode == HttpStatusCode.NotFound)
                {
                    _logger.LogInformation("Controller does not exist on server side. Unenrolling client.");
                    await Unenroll();

                    return;
                }

                message.EnsureSuccessStatusCode();
                CabinetController controller = JsonConvert.DeserializeObject <CabinetController>(await message.Content.ReadAsStringAsync());

                _logger.LogInformation("Client enrollment state is valid.");
            }
            catch (Exception e)
            {
                _logger.LogError("Unable to verify the controller enrollment status. The connection will be retried.", e);
                autoReconnectTimer.Start();
            }

            // Connect via SignalR.
            try
            {
                _logger.LogInformation("Connecting to SignalR.");

                hubConnection = hubConnectionBuilder.Build();

                await hubConnection.StartAsync();

                autoReconnectTimer.Stop();

                hubConnection.On <ScheduledCommand>("RecieveCommand", command =>
                {
                    Task.Run(() => HandleCommand(command));
                });

                hubConnection.Closed += HubConnection_Closed;
                Connected             = true;

                _logger.LogInformation("Connected to SignalR successfully.");
            }
            catch (Exception e)
            {
                _logger.LogError("Error connecting to SignalR. The connection will be retried.", e);
                autoReconnectTimer.Start();
            }
        }
 public static TypedHubClient <THub> Build <THub>(this IHubConnectionBuilder connectionBuilder) where THub : class
 {
     return(new TypedHubClient <THub>(connectionBuilder.Build()));
 }