MQTTnet
MQTTnet copied to clipboard
how to unsubscibe all topics
MqttTest.zip
`var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
.Build();
//MqttClientUnsubscribeOptions mqttClientUnsubscribeOptions = new MqttClientUnsubscribeOptions();
//mqttClientUnsubscribeOptions.TopicFilters = mqttClient.Options.
//await mqttClient.UnsubscribeAsync(mqttClientUnsubscribeOptions);
mqttSubscribeOptions.TopicFilters = mqttTopicFilters;
var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);`
when I click Subscribe several times,I received several identical messages at the same time, Can't it remove duplicate topics,or how to unsbuscribe all topics?
You can subsribe to the same topic multiple times if you want, but you shouldn't. And you can unsubscribe using UnsubscribeAsync, so what's the point?
@lujiashun1 As far as I understand your problem, you want to avoid duplicate subscribtions. So what can you do is to create some hash map and store there what topics have been subscibed already and before making new sub you just will check if you haven't already subscribed.
PS. Use lock statement that no parallel clicks subscibe to one topic twice.
The MQTT protocol requires that the user keeps track of subscriptions and make proper calls and checks to keep them clean.