0

I have a device that has a built-in mqtt client that subscribes to a broker server and displays topic 0 or 1;

  • 0 - disabled;
  • 1 - enabled;

MQTT Broker (ASP.NET WEB API) .Net 6

BUILDER

var optionBuilder = new MqttServerOptionsBuilder()
    .WithDefaultEndpoint()
    .WithDefaultCommunicationTimeout(TimeSpan.FromMilliseconds(5000))
    .Build();

builder.Services
    .AddHostedMqttServer(optionBuilder)
    .AddMqttConnectionHandler()
    .AddConnections()
    .AddMqttTcpServerAdapter();

builder.Services.AddMqttConnectionHandler();
builder.Services.AddMqttWebSocketServerAdapter();

APP

app.UseMqttServer(server =>
{
});

After connecting the device to the server, I want to see the status of this client on the server and send a parameter to change the topic attribute.

In version 3.. - I used the IMqttServer interface

private readonly IMqttServer _mqttServer;

        public MqttBrokerService(IMqttServer mqttServer)
        {
            _mqttServer = mqttServer ?? throw new ArgumentNullException(nameof(mqttServer));
        }

        public Task<IList<IMqttClientStatus>> GetClientStatusAsync()
        {
            return _mqttServer.GetClientStatusAsync();
        }

        public Task<IList<IMqttSessionStatus>> GetSessionStatusAsync()
        {
            return _mqttServer.GetSessionStatusAsync();
        }

        public Task ClearRetainedApplicationMessagesAsync()
        {
            return _mqttServer.ClearRetainedApplicationMessagesAsync();
        }

        public Task<IList<MqttApplicationMessage>> GetRetainedApplicationMessagesAsync()
        {
            return _mqttServer.GetRetainedApplicationMessagesAsync();
        }

        public Task<MqttClientPublishResult> PublishAsync(MqttApplicationMessage applicationMessage)
        {
            if (applicationMessage == null)
            {
                throw new ArgumentNullException(nameof(applicationMessage));
            }

            return _mqttServer.PublishAsync(applicationMessage);
        }

But in version 4.. - this interface was removed and now I don't understand how I can build messages for the client and get detailed statistics.

there is MQTTnet.Extensions.ManagedClient, but I still could not connect to the active session of my client.

var options = new ManagedMqttClientOptionsBuilder()
    .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
    .WithClientOptions(new MqttClientOptionsBuilder()
        .WithClientId("Client1")
        .WithTcpServer("192.168.1.1")
        .WithTls().Build())
    .Build();

var mqttClient = new MqttFactory().CreateManagedMqttClient();
await mqttClient.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic("my/topic").Build());
await mqttClient.StartAsync(options);

I will be very grateful for your help

itprodavets
  • 69
  • 3
  • 9
  • Did you ever work it out? I'm able to get a broker integrated in my website and my test MqttClient will be able to communicate with the broker just fine but only if the website runs from Visual Studio directly, seemingly by it using IISExpress. However if I publish it to IIS and go to the site the MqttClient can't talk to the broker, seems like it can't even see it. – Flood Jul 25 '22 at 21:53
  • Yes, this work. You can use, but needs set port for mqtt – itprodavets Jan 27 '23 at 12:43

0 Answers0