rafiulgits/mqtt-client-dotnet-core

Client is disconnected

Closed this issue · 6 comments

It is working perfectly, but it can happen that all calls to the mqtt service have a 500 response, with error message, client is not connected.
The only way to solve this is to restart the website, this can not be the intention off course.
How is this handled?

can you please describe your issue with your use case including some screenshots. I had used this in many of my projects I didn't faced anything like this.

Is it IIS recycling?

@quan0zhou not really, this is an experimental implementation of MQTT in .NET core framework. I deployed this app in linux machine through linux systemd service, where I managed restarted parameter to restart the app service when it crashed or stopped.

Personally, it's better to try again
https://github.com/dotnet/MQTTnet/blob/master/Samples/Client/Client_Connection_Samples.cs

  public static void Reconnect_Using_Timer()
    {
        /*
         * This sample shows how to reconnect when the connection was dropped.
         * This approach uses a custom Task/Thread which will monitor the connection status.
         * This is the recommended way but requires more custom code!
         */

        var mqttFactory = new MqttFactory();

        using (var mqttClient = mqttFactory.CreateMqttClient())
        {
            var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();

            _ = Task.Run(
                async () =>
                {
                    // User proper cancellation and no while(true).
                    while (true)
                    {
                        try
                        {
                            // This code will also do the very first connect! So no call to _ConnectAsync_ is required in the first place.
                            if (!await mqttClient.TryPingAsync())
                            {
                                await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

                                // Subscribe to topics when session is clean etc.
                                Console.WriteLine("The MQTT client is connected.");
                            }
                        }
                        catch
                        {
                            // Handle the exception properly (logging etc.).
                        }
                        finally
                        {
                            // Check the connection state every 5 seconds and perform a reconnect if required.
                            await Task.Delay(TimeSpan.FromSeconds(5));
                        }
                    }
                });
        }
    }

@quan0zhou Your code script for console application, it would be better if you find that solution for .NET core framework. Although my codebase is outdated, currently upgraded MQTT package available. I can't maintain this codebase regularly.

So, if you find out a solution a create a pull request, it will be great.

@Navorski please check current codebase, reconnect feature has been added.