MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

ManagedClient does not auto reconnect after wifi is reestablished

Open albrecht-jun-zhang opened this issue 6 years ago • 4 comments

Sample ManagedClientTest (3.0.8) below does not reconnect automatically after Wifi is disabled and reenabled. Any help will be appreciated.

Update: Actually ManagedClient only recovers mqtt connection successfully under dot net platform. Under UWP or Xamarin Forms / mono platform, recovery of mqtt connection fails when reestablishing the internet connection

public static async Task RunAsync()
        {
            var ms = new ClientRetainedMessageHandler();

            var options = new ManagedMqttClientOptions
            {
                ClientOptions = new MqttClientOptions
                {
                    ClientId = "MQTTnetManagedClientTest",
                    Credentials = new RandomPassword(),
                    ChannelOptions = new MqttClientTcpOptions
                    {
                        Server = "broker.hivemq.com"
                    }
                },

                AutoReconnectDelay = TimeSpan.FromSeconds(1),
                Storage = ms
            };

            try
            {
                var managedClient = new MqttFactory().CreateManagedMqttClient();
                managedClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
                {
                    Console.WriteLine(">> RECEIVED: " + e.ApplicationMessage.Topic);
                });

                managedClient.UseConnectedHandler(e => {
                    Console.WriteLine("connected..."); // Unfortunately connected won't get called after wifi is reestablished
                });

                await managedClient.StartAsync(options);

                await managedClient.SubscribeAsync(new TopicFilter { Topic = "xyz", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
                await managedClient.SubscribeAsync(new TopicFilter { Topic = "abc", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });

                await managedClient.PublishAsync(builder => builder.WithTopic("Step").WithPayload("3"));

                Console.WriteLine("Managed client started.");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

albrecht-jun-zhang avatar Oct 30 '19 12:10 albrecht-jun-zhang

@JanEggers You're the expert for UWP I would say :D

SeppPenner avatar Nov 07 '19 07:11 SeppPenner

@SeppPenner no sry I dont have a testenvironment for that. can you provide any logs?

JanEggers avatar Nov 07 '19 11:11 JanEggers

no sry I dont have a testenvironment for that.

Same for me. I did use UWP some day but had to do a new setup of my computer and didn't install it anymore.

SeppPenner avatar Nov 08 '19 07:11 SeppPenner

Problem still exists for me. Has anybody found a solution?

ArthurGrot avatar May 03 '21 11:05 ArthurGrot

Please try version 4.1.2.350 from the myget feed and let me know if the issue is fixed.

chkr1011 avatar Oct 23 '22 08:10 chkr1011

The fix for this issue is now released.

chkr1011 avatar Nov 11 '22 13:11 chkr1011