示例#1
0
 public static async Task <MqttClientSubscribeResult> SubscribeAsync(this IMqttPersisterConnection persisterConnection, string topic, CancellationToken cancellationToken = default)
 {
     if (await persisterConnection.TryConnectAsync(afterDisconnection: false, cancellationToken))
     {
         return(await persisterConnection.GetClient().SubscribeAsync(topic));
     }
     return(null);
 }
示例#2
0
 public static async Task <bool> TryRegisterMessageHandlerAsync(this IMqttPersisterConnection persisterConnection, Func <MqttApplicationMessageReceivedEventArgs, Task> handler, CancellationToken cancellationToken = default)
 {
     if (await persisterConnection.TryConnectAsync(afterDisconnection: false, cancellationToken))
     {
         persisterConnection.GetClient().UseApplicationMessageReceivedHandler(handler);
         return(true);
     }
     return(false);
 }
示例#3
0
 public async Task OnConnectionLostAsync(IMqttPersisterConnection connection, MqttClientConnectionEventArgs args)
 {
     if (args != null && args.IsReConnected)
     {
         if (args.DisconnectReason == Client.Disconnecting.MqttClientDisconnectReason.NormalDisconnection)
         {
             await ReSubscribeAllAsync();
         }
     }
 }
        public MqttEventBus(IMqttPersisterConnection persistentConnection, ILogger <MqttEventBus> logger,
                            IServiceScopeFactory scopeFactory, IEventBusSubscriptionsManager subsManager, int retryCount = 5)
        {
            _persistentConnection = persistentConnection ?? throw new ArgumentNullException(nameof(persistentConnection));
            _logger      = logger ?? throw new ArgumentNullException(nameof(logger));
            _subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager();
            _mqttClient  = _persistentConnection.GetClient();
            _mqttClient.UseApplicationMessageReceivedHandler(e => Consumer_Received(e));
            _retryCount   = retryCount;
            _scopeFactory = scopeFactory;

            //_subsManager.OnEventRemoved += SubsManager_OnEventRemoved;
        }
示例#5
0
 public MqttEventBus(
     IMqttPersisterConnection mqttPersisterConnection,
     IEventProvider eventProvider,
     IConsumeMethodInvoker consumeMethodInvoker,
     IEventBusLogger <MqttEventBus> logger,
     IServiceScopeFactory scopeFactory,
     ISubscriptionsManager subsManager,
     BusOptions busOptions)
 {
     _mqttPersisterConnection = mqttPersisterConnection ?? throw new ArgumentNullException(nameof(mqttPersisterConnection));
     _mqttPersisterConnection.ClientConnectionChanged += OnConnectionLostAsync;
     _eventProvider        = eventProvider;
     _consumeMethodInvoker = consumeMethodInvoker;
     _logger       = logger ?? throw new ArgumentNullException(nameof(logger));
     _subsManager  = subsManager ?? throw new ArgumentNullException(nameof(ISubscriptionsManager));
     _retryCount   = busOptions?.RetryCount ?? 5;
     _scopeFactory = scopeFactory;
     _asyncLocker  = new SemaphoreSlim(busOptions.MaxConcurrentCalls, busOptions.MaxConcurrentCalls);
 }