confirmSubscription method

bool confirmSubscription(
  1. MqttMessage? msg
)

Confirms a subscription has been made with the broker. Marks the sub as confirmed in the subs storage. Returns true on successful subscription, false on fail.

Implementation

bool confirmSubscription(MqttMessage? msg) {
  final subAck = msg as MqttSubscribeAckMessage;
  String topic;
  if (pendingSubscriptions
      .containsKey(subAck.variableHeader!.messageIdentifier)) {
    topic = pendingSubscriptions[subAck.variableHeader!.messageIdentifier]!
        .topic
        .rawTopic;
    subscriptions[topic] =
        pendingSubscriptions[subAck.variableHeader!.messageIdentifier];
    pendingSubscriptions.remove(subAck.variableHeader!.messageIdentifier);
  } else {
    return false;
  }

  // Check the Qos, we can get a failure indication(value 0x80) here if the
  // topic cannot be subscribed to.
  if (subAck.payload.qosGrants.isEmpty ||
      subAck.payload.qosGrants[0] == MqttQos.failure) {
    subscriptions.remove(topic);
    if (onSubscribeFail != null) {
      onSubscribeFail!(topic);
      return false;
    }
  }
  // Success, call the subscribed callback
  if (onSubscribed != null) {
    onSubscribed!(topic);
  }
  return true;
}