Skip to content

Add MqttClient::setCleanSession(...) API #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -84,6 +84,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 @@ -124,6 +126,7 @@ class MqttClient : public Client {
String _id;
String _username;
String _password;
bool _cleanSession;

unsigned long _keepAliveInterval;
unsigned long _connectionTimeout;
Expand Down