Dynamic input of topics #2015
Closed
gabrielvictorafreitas
started this conversation in
General
Replies: 2 comments 2 replies
-
Done! var mqttSubscribeOptions = GenerateSubscriptions();
// Subscribe to all topics
foreach (var topic in mqttSubscribeOptions)
{
await mqttClient.SubscribeAsync(topic, cancellationToken).ConfigureAwait(false);
}
private List<MqttClientSubscribeOptions> GenerateSubscriptions()
{
List<MqttClientSubscribeOptions> options = new List<MqttClientSubscribeOptions>();
List<string> topics = new List<string>(topic.Split('|'));
var factory = new MqttFactory();
foreach (var t in topics)
{
options.Add(factory.CreateSubscribeOptionsBuilder()
.WithTopicFilter(
f =>
{
f.WithTopic(t);
})
.Build());
}
return options;
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
I guess, you need to do it like that: var subscribeResults = new MqttClientSubscribeResult[topics.Length];
for (var i = 0; i < topics.Length; i++)
{
var idx = i;
var mqttClientSubscribeOptions = this.mqttFactory.CreateSubscribeOptionsBuilder()
.WithTopicFilter(
f =>
{
f.WithTopic(topics[idx]);
})
.Build();
var subscribeResult = await this.mqttClient.SubscribeAsync(mqttClientSubscribeOptions, cancellationToken ?? CancellationToken.None);
subscribeResults[idx] = subscribeResult;
}
return subscribeResults;
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Does anyone know if i can have a Dynamic number of topics? My ideia is to configure the topics at appsettings and do like pipe sepparately pattern?
I know that we need to configure something like this:
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder() .WithTopicFilter( f => { f.WithTopic("mqttnet/samples/topic/1"); }) .WithTopicFilter( f => { f.WithTopic("mqttnet/samples/topic/2").WithNoLocal(); }) .WithTopicFilter( f => { f.WithTopic("mqttnet/samples/topic/3").WithRetainHandling(MqttRetainHandling.SendAtSubscribe); }) .Build();
my ideia is to do something like this:
`List topics = new List(topic.Split('|'));
I know that this configuration is wrong
Beta Was this translation helpful? Give feedback.
All reactions