Skip to content

Commit 7c9e71a

Browse files
committed
Add MqttClient::setCleanSession(...) API
1 parent b4468ef commit 7c9e71a

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

Diff for: examples/WiFiAdvancedCallback/WiFiAdvancedCallback.ino

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ void setup() {
6969
// You can provide a username and password for authentication
7070
// mqttClient.setUsernamePassword("username", "password");
7171

72+
// By default the library connects with the "clean session" flag set,
73+
// you can disable this behaviour by using
74+
// mqttClient.setCleanSession(false);
75+
7276
// set a will message, used by the broker when the connection dies unexpectantly
7377
// you must know the size of the message before hand, and it must be set before connecting
7478
String willPayload = "oh no!";

Diff for: keywords.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ connected KEYWORD2
4040

4141
setId KEYWORD2
4242
setUsernamePassword KEYWORD2
43-
43+
setCleanSession KEYWORD2
4444
setKeepAliveInterval KEYWORD2
4545
setConnectionTimeout KEYWORD2
4646

Diff for: src/MqttClient.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ enum {
6464
MqttClient::MqttClient(Client& client) :
6565
_client(&client),
6666
_onMessage(NULL),
67+
_cleanSession(true),
6768
_keepAliveInterval(60 * 1000L),
6869
_connectionTimeout(30 * 1000L),
6970
_connectError(MQTT_SUCCESS),
@@ -772,6 +773,11 @@ void MqttClient::setUsernamePassword(const String& username, const String& passw
772773
_password = password;
773774
}
774775

776+
void MqttClient::setCleanSession(bool cleanSession)
777+
{
778+
_cleanSession = cleanSession;
779+
}
780+
775781
void MqttClient::setKeepAliveInterval(unsigned long interval)
776782
{
777783
_keepAliveInterval = interval;
@@ -855,7 +861,10 @@ int MqttClient::connect(IPAddress ip, const char* host, uint16_t port)
855861
}
856862

857863
flags |= _willFlags;
858-
flags |= 0x02; // clean session
864+
865+
if (_cleanSession) {
866+
flags |= 0x02; // clean session
867+
}
859868

860869
connectVariableHeader.protocolName.length = htons(sizeof(connectVariableHeader.protocolName.value));
861870
memcpy(connectVariableHeader.protocolName.value, "MQTT", sizeof(connectVariableHeader.protocolName.value));

Diff for: src/MqttClient.h

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class MqttClient : public Client {
8484
void setUsernamePassword(const char* username, const char* password);
8585
void setUsernamePassword(const String& username, const String& password);
8686

87+
void setCleanSession(bool cleanSession);
88+
8789
void setKeepAliveInterval(unsigned long interval);
8890
void setConnectionTimeout(unsigned long timeout);
8991

@@ -124,6 +126,7 @@ class MqttClient : public Client {
124126
String _id;
125127
String _username;
126128
String _password;
129+
bool _cleanSession;
127130

128131
unsigned long _keepAliveInterval;
129132
unsigned long _connectionTimeout;

0 commit comments

Comments
 (0)