Skip to content

Commit

Permalink
MqttClient - fix local disconnect after pulish + ka
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaturn committed Jan 3, 2023
1 parent 09e3a3e commit baffda8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/TinyMqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ MqttClient::MqttClient(MqttBroker* local_broker, TcpClient* new_client)
MqttClient::MqttClient(MqttBroker* local_broker, const std::string& id)
: local_broker(local_broker), clientId(id)
{
if (local_broker) local_broker->addClient(this);
alive = 0;
keep_alive = 0;

if (local_broker) local_broker->addClient(this);
}

MqttClient::~MqttClient()
Expand Down Expand Up @@ -281,7 +284,7 @@ void MqttClient::clientAlive(uint32_t more_seconds)

void MqttClient::loop()
{
if (alive && (millis() > alive))
if (keep_alive && (millis() >= alive))
{
if (local_broker)
{
Expand Down
28 changes: 28 additions & 0 deletions tests/local-tests/local-tests.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,34 @@ test(local_client_should_unregister_when_destroyed)
assertEqual(broker.clientsCount(), (size_t)0);
}

test(local_client_do_not_disconnect_after_publishing)
{
set_millis(0);
MqttBroker broker(1883);
MqttClient client(&broker, "client");
MqttClient sender(&broker, "sender");
broker.loop();

client.subscribe("#");
client.subscribe("test");
client.setCallback(onPublish);
assertEqual(broker.clientsCount(), (size_t)2);

sender.publish("test", "value");
broker.loop();

add_seconds(60);
client.loop();
sender.loop();
broker.loop();

assertEqual(broker.clientsCount(), (size_t)2);
assertEqual(sender.connected(), true);
assertEqual(client.connected(), true);

assertEqual(published.size(), (size_t)1); // client has received something
}

#if 0
test(local_connect)
{
Expand Down

0 comments on commit baffda8

Please sign in to comment.