public async Task UnsubscribeAsync(string topic, IMqttMessageListener listener) { CheckOpen(); MqttSubscription deletedSubscription = null; var hasMoreSubscriptions = false; lock (_lock) { // Find subscription for (var index = 0; index < _subscriptions.Count; index++) { var subscription = _subscriptions[index]; if (subscription.Topic == topic && subscription.Listener == listener) { deletedSubscription = subscription; _subscriptions.RemoveAt(index); break; } } // Find subscriptions to the same topic foreach (var subscription in _subscriptions) { if (subscription.Topic == topic) { hasMoreSubscriptions = true; } } } // Unsubscribe if there are no more subscriptions if (!hasMoreSubscriptions) { await _connection.UnsubscribeAsync(topic); } }
public async Task SubscribeAsync(string topic, MqttQualityOfServiceLevel qos, IMqttMessageListener listener) { CheckOpen(); // Subscribe to the topic // Todo: Shall we skip if similar subscription already exist? await _connection.SubscribeAsync(topic, qos); lock (_lock) { var filter = topic.IndexOf("*") < 0; // Add subscription to the list var subscription = new MqttSubscription { Topic = topic, Listener = listener, Filter = filter, Qos = qos }; _subscriptions.Add(subscription); } }