/// <summary> /// Unsubscribe for message topics /// </summary> /// <param name="topics">List of topics to unsubscribe</param> /// <returns>Message Id in UNSUBACK message from broker</returns> public ushort Unsubscribe(string[] topics) { int attempts = 0; bool acknowledged = false; MqttMsgUnsubscribe unsubscribe = new MqttMsgUnsubscribe(topics); MqttMsgUnsuback unsuback = null; do { try { // try unsubscribe unsuback = (MqttMsgUnsuback)this.SendReceive(unsubscribe.GetBytes()); acknowledged = true; } catch (MqttTimeoutException) { // no UNSUBACK message received in time, retry with duplicate flag attempts++; unsubscribe.DupFlag = true; // delay before retry if (attempts < MQTT_ATTEMPTS_RETRY) Thread.Sleep(MQTT_DELAY_RETRY); } } while ((attempts < MQTT_ATTEMPTS_RETRY) && !acknowledged); // return message id from SUBACK or zero (no message id) return acknowledged ? unsuback.MessageId : (ushort)0; }