Skip to content

Commit

Permalink
Merge pull request arduino-libraries#12 from sandeepmistry/set-clean-…
Browse files Browse the repository at this point in the history
…session

Add MqttClient::setCleanSession(...) API
  • Loading branch information
aentinger authored Mar 18, 2020
2 parents 4858270 + 7c9e71a commit 597f62a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions examples/WiFiAdvancedCallback/WiFiAdvancedCallback.ino
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ void setup() {
// You can provide a username and password for authentication
// mqttClient.setUsernamePassword("username", "password");

// By default the library connects with the "clean session" flag set,
// you can disable this behaviour by using
// mqttClient.setCleanSession(false);

// set a will message, used by the broker when the connection dies unexpectantly
// you must know the size of the message before hand, and it must be set before connecting
String willPayload = "oh no!";
Expand Down
2 changes: 1 addition & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ connected KEYWORD2

setId KEYWORD2
setUsernamePassword KEYWORD2

setCleanSession KEYWORD2
setKeepAliveInterval KEYWORD2
setConnectionTimeout KEYWORD2

Expand Down
11 changes: 10 additions & 1 deletion src/MqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ enum {
MqttClient::MqttClient(Client& client) :
_client(&client),
_onMessage(NULL),
_cleanSession(true),
_keepAliveInterval(60 * 1000L),
_connectionTimeout(30 * 1000L),
_connectError(MQTT_SUCCESS),
Expand Down Expand Up @@ -772,6 +773,11 @@ void MqttClient::setUsernamePassword(const String& username, const String& passw
_password = password;
}

void MqttClient::setCleanSession(bool cleanSession)
{
_cleanSession = cleanSession;
}

void MqttClient::setKeepAliveInterval(unsigned long interval)
{
_keepAliveInterval = interval;
Expand Down Expand Up @@ -855,7 +861,10 @@ int MqttClient::connect(IPAddress ip, const char* host, uint16_t port)
}

flags |= _willFlags;
flags |= 0x02; // clean session

if (_cleanSession) {
flags |= 0x02; // clean session
}

connectVariableHeader.protocolName.length = htons(sizeof(connectVariableHeader.protocolName.value));
memcpy(connectVariableHeader.protocolName.value, "MQTT", sizeof(connectVariableHeader.protocolName.value));
Expand Down
3 changes: 3 additions & 0 deletions src/MqttClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class MqttClient : public Client {
void setUsernamePassword(const char* username, const char* password);
void setUsernamePassword(const String& username, const String& password);

void setCleanSession(bool cleanSession);

void setKeepAliveInterval(unsigned long interval);
void setConnectionTimeout(unsigned long timeout);

Expand Down Expand Up @@ -128,6 +130,7 @@ class MqttClient : public Client {
String _id;
String _username;
String _password;
bool _cleanSession;

unsigned long _keepAliveInterval;
unsigned long _connectionTimeout;
Expand Down

0 comments on commit 597f62a

Please sign in to comment.