/// <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); }
public void UnbscribeBasicEncodeTestv5() { // Arrange byte[] encodedCorrect = new byte[] { 162, 19, 0, 42, 0, 0, 6, 116, 112, 111, 105, 99, 49, 0, 6, 116, 111, 112, 105, 99, 50 }; MqttMsgUnsubscribe unsubscribe = new MqttMsgUnsubscribe(new string[] { "tpoic1", "topic2" }); unsubscribe.MessageId = 42; // Act byte[] encoded = unsubscribe.GetBytes(MqttProtocolVersion.Version_5); // Assert Assert.Equal(encodedCorrect, encoded); }
public void UnbscribeAdvanceEncodeTestv5() { // Arrange byte[] encodedCorrect = new byte[] { 162, 54, 0, 42, 35, 38, 0, 4, 80, 114, 111, 112, 0, 26, 111, 110, 108, 121, 32, 111, 110, 101, 32, 116, 104, 105, 115, 32, 116, 105, 109, 101, 32, 102, 111, 114, 32, 102, 117, 110, 0, 6, 116, 112, 111, 105, 99, 49, 0, 6, 116, 111, 112, 105, 99, 50 }; MqttMsgUnsubscribe unsubscribe = new MqttMsgUnsubscribe(new string[] { "tpoic1", "topic2" }); unsubscribe.MessageId = 42; unsubscribe.UserProperties.Add(new UserProperty("Prop", "only one this time for fun")); // Act byte[] encoded = unsubscribe.GetBytes(MqttProtocolVersion.Version_5); Helpers.DumpBuffer(encoded); // Assert Assert.Equal(encodedCorrect, encoded); }