public async Task OnConnectionLostAsync(IMqttPersisterConnection connection, MqttClientConnectionEventArgs args)
 {
     if (args != null && args.IsReConnected)
     {
         if (args.DisconnectReason == Client.Disconnecting.MqttClientDisconnectReason.NormalDisconnection)
         {
             await ReSubscribeAllAsync();
         }
     }
 }
Exemple #2
0
        private Task InvokeClientConnectionChangedMethod(MqttClientConnectionEventArgs args)
        {
            var handler = ClientConnectionChanged;

            if (handler != null)
            {
                return(handler.Invoke(this, args));
            }
            return(Task.CompletedTask);
        }
Exemple #3
0
        private Task OnConnectedAsync(MqttClientConnectedEventArgs e)
        {
            if (_disconnectionCache.TryGetValue(_options.ClientId, out var args))
            {
                if (!args.IsReConnected)
                {
                    _logger.LogInformation($"A MqttServer '{_options.ClientId}' trying to connect...");
                    args.MarkAsConnected();
                    return(InvokeClientConnectionChangedMethod(args));
                }
            }
            else
            {
                _disconnectionCache.Add(_options.ClientId, MqttClientConnectionEventArgs.Connected(_options.ClientId));
            }

            return(Task.CompletedTask);
        }
Exemple #4
0
        private async Task OnDisconnectedAsync(MqttClientDisconnectedEventArgs e)
        {
            if (_disposed)
            {
                return;
            }

            _logger.LogWarning($"A MqttServer '{_options.ClientId}' connection is shutdown, Reason: {e.Reason}. Trying to re-connect...");
            if (_disconnectionCache.TryGetValue(_options.ClientId, out var args))
            {
                if (args.IsReConnected)
                {
                    args.MarkAsDisconnected();
                    await InvokeClientConnectionChangedMethod(args);
                }
            }
            else
            {
                _disconnectionCache.Add(_options.ClientId, MqttClientConnectionEventArgs.Disconnected(_options.ClientId, e.Reason));
                await InvokeClientConnectionChangedMethod(args);
            }

            await TryConnectAsync(true);
        }