Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7c9e71a

Browse filesBrowse files
committed
Add MqttClient::setCleanSession(...) API
1 parent b4468ef commit 7c9e71a
Copy full SHA for 7c9e71a

File tree

Expand file treeCollapse file tree

4 files changed

+18
-2
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+18
-2
lines changed

‎examples/WiFiAdvancedCallback/WiFiAdvancedCallback.ino

Copy file name to clipboardExpand all lines: examples/WiFiAdvancedCallback/WiFiAdvancedCallback.ino
+4Lines changed: 4 additions & 0 deletions
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!";

‎keywords.txt

Copy file name to clipboardExpand all lines: keywords.txt
+1-1Lines changed: 1 addition & 1 deletion
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

‎src/MqttClient.cpp

Copy file name to clipboardExpand all lines: src/MqttClient.cpp
+10-1Lines changed: 10 additions & 1 deletion
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));

‎src/MqttClient.h

Copy file name to clipboardExpand all lines: src/MqttClient.h
+3Lines changed: 3 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.