Esempio n. 1
0
        public async Task CreateConnection()
        {
            _connection = new HubConnectionBuilder().WithUrl(_hubUrl).Build();

            _connection.Closed += async(exception) =>
            {
                retries++;
                if (retries == MaxRetries)
                {
                    return;
                }
                _logger.LogWarning("Disconnected attempting to reconnect");
                await Connect();
            };

            await Connect();

            _connection.On("connectionSucceeded", () =>
            {
                ConnectionStatusUpdated?.Invoke(this, new ConnectionStatusUpdatedEventArgs(true));
                _logger.LogInformation("Connection accepted");
            });

            _connection.On <string>("connectionFailed", (err) =>
            {
                ConnectionStatusUpdated?.Invoke(this, new ConnectionStatusUpdatedEventArgs(false, err));
                _logger.LogWarning("Connection refused for reason: " + err);
            });

            _connection.On <Success>("operationComplete", success =>
            {
                var operation = new OperationMessage(success.OperationId, success.Status);
                MessageReceived?.Invoke(this, operation);
            });

            _connection.On <Failure>("operationFailed", failure =>
            {
                var operation = new OperationMessageFailed(failure.OperationId, failure.Status, failure.Code, failure.Reason);
                MessageReceived?.Invoke(this, operation);
            });
        }
Esempio n. 2
0
        public async Task CreateConnection(string url)
        {
            _connection = new HubConnectionBuilder().WithUrl(url).Build();

            _connection.Closed += async(exceptioon) =>
            {
                _logger.LogWarning("Connection lost, retrying for reason: " + exceptioon.Message);
                ConnectionStatusUpdated.Invoke(false);
                await Connect();
            };

            _connection.On <Success>("operationComplete", success =>
            {
                var operation = new OperationMessage(success.OperationId, success.Status);
                _successCallbacks.ForEach(callback => callback.Invoke(operation));
            });

            _connection.On <Failure>("operationFailed", failure =>
            {
                var operation = new OperationFailed(failure.OperationId, failure.Status, failure.Reason, failure.Code);
                _failedCallbacks.ForEach(callback => callback.Invoke(operation));
            });

            _connection.On("connectionSucceeded", () =>
            {
                _logger.LogInformation("connection and verification successful.");
                ConnectionStatusUpdated?.Invoke(true);
            });

            _connection.On <string>("connectionFailed", (reason) =>
            {
                ConnectionStatusUpdated?.Invoke(false);
                _logger.LogWarning("Connection failed for reason: " + reason);
            });

            await Connect();
        }
Esempio n. 3
0
 protected void OnStatusUpdated(string status)
 {
     CoreApplication.MainView.Dispatcher.RunIdleAsync(_ =>
                                                      ConnectionStatusUpdated?.Invoke(this, status)
                                                      );
 }