[issue #84] Fix and unit test added
This commit is contained in:
@@ -646,6 +646,7 @@ void MqttClient::processMessage(MqttMessage* mesg)
|
|||||||
if (mqtt_connected() or tcp_client == nullptr)
|
if (mqtt_connected() or tcp_client == nullptr)
|
||||||
{
|
{
|
||||||
uint8_t qos = mesg->flags();
|
uint8_t qos = mesg->flags();
|
||||||
|
qos = (qos / 2) & 3;
|
||||||
payload = header;
|
payload = header;
|
||||||
mesg->getString(payload, len);
|
mesg->getString(payload, len);
|
||||||
Topic published(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;
|
Console << "Received Publish (" << published.str().c_str() << ") size=" << (int)len << endl;
|
||||||
#endif
|
#endif
|
||||||
// << '(' << string(payload, len).c_str() << ')' << " msglen=" << mesg->length() << endl;
|
// << '(' << 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;
|
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 DUP
|
||||||
// TODO reset RETAIN
|
// TODO reset RETAIN
|
||||||
|
|
||||||
|
|||||||
@@ -349,6 +349,39 @@ test(retained_message)
|
|||||||
assertEqual(published.size(), (size_t)2);
|
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)
|
test(remote_client_disconnect_reconnect)
|
||||||
{
|
{
|
||||||
published.clear();
|
published.clear();
|
||||||
|
|||||||
Reference in New Issue
Block a user