diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index 939528f..dbaa9f9 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -646,6 +646,7 @@ void MqttClient::processMessage(MqttMessage* mesg) if (mqtt_connected() or tcp_client == nullptr) { uint8_t qos = mesg->flags(); + qos = (qos / 2) & 3; payload = header; mesg->getString(payload, len); Topic published(payload, len); @@ -654,8 +655,19 @@ void MqttClient::processMessage(MqttMessage* mesg) Console << "Received Publish (" << published.str().c_str() << ") size=" << (int)len << endl; #endif // << '(' << string(payload, len).c_str() << ')' << " msglen=" << mesg->length() << endl; - if (qos) payload+=2; // ignore packet identifier if any + const char* ID; // remove PublishID() to avoid misuse + if (qos) { + ID = payload; + payload+=2; // ignore packet identifier if any + } len=mesg->end()-payload; + if (qos == 1) + { + MqttMessage msg(MqttMessage::Type::PubAck); + msg.add(ID[0]); // MessageID high + msg.add(ID[1]); // MessageID low + msg.sendTo(this); + } // TODO reset DUP // TODO reset RETAIN diff --git a/tests/network-tests/network-tests.ino b/tests/network-tests/network-tests.ino index 2528c96..d0169eb 100644 --- a/tests/network-tests/network-tests.ino +++ b/tests/network-tests/network-tests.ino @@ -349,6 +349,39 @@ test(retained_message) assertEqual(published.size(), (size_t)2); } +test(retained_payload) // # issue #84 +{ + published.clear(); + + start_many_wifi_esp(2, true); + assertEqual(WiFi.status(), WL_CONNECTED); + + MqttBroker broker(1883); + broker.begin(); + broker.retain(10); + IPAddress broker_ip = WiFi.localIP(); + + MqttClient local_client(&broker, "sender"); + + // Send a retained message + // No remote client connected + local_client.publish("topic", "retained", true); + for(int i=0; i<2; i++) { broker.loop(); local_client.loop(); }; + + // No connect a client from 2nd Esp + ESP8266WiFiClass::selectInstance(2); + MqttClient remote_client("receiver"); + remote_client.connect(broker_ip, 1883); + remote_client.setCallback(onPublish); + remote_client.subscribe("#"); + + assertTrue(remote_client.connected()); + for(int i=0; i<10; i++) { broker.loop(); local_client.loop(); remote_client.loop(); }; + + // Check that the retained message is published + assertEqual(lastPayload, "retained"); +} + test(remote_client_disconnect_reconnect) { published.clear();