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