Skip to content

added subscribeMultple #341

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -244,6 +244,15 @@ bool subscribe(const char topic[], int qos);

- The functions return a boolean that indicates if the subscription has been successful (true).

Subscribe to multiple topics:

```c++
bool subscribeMultiple(int count, const char *topics[], int qos_levels[]);
```

- The functions allow subscribing to multiple topics at once.
- The functions return a boolean that indicates if the subscription has been successful (true).

Unsubscribe from a topic:

```c++
@@ -253,6 +262,15 @@ bool unsubscribe(const char topic[]);

- The functions return a boolean that indicates if the unsubscription has been successful (true).

Unsubscribe from multiple topics:

```c++
bool unsubscribeMultiple(int count, const char *topics[]);
```

- The functions allow unsubscribing from multiple topics at once.
- The functions return a boolean that indicates if the unsubscription has been successful (true).

Sends and receives packets:

```c++
46 changes: 46 additions & 0 deletions src/MQTTClient.cpp
Original file line number Diff line number Diff line change
@@ -441,6 +441,30 @@ bool MQTTClient::subscribe(const char topic[], int qos) {
return true;
}

bool MQTTClient::subscribeMultiple(int count, const char *topics[], int qos_levels[]) {
// return immediately if not connected
if (!this->connected()) {
return false;
}

lwmqtt_string_t topic_filters[count];
lwmqtt_qos_t qos[count];

for (int i = 0; i < count; i++) {
topic_filters[i] = lwmqtt_string(topics[i]);
qos[i] = (lwmqtt_qos_t)qos_levels[i];
}

this->_lastError = lwmqtt_subscribe(&this->client, count, topic_filters, qos, this->timeout);
if (this->_lastError != LWMQTT_SUCCESS) {
// close connection
this->close();
return false;
}

return true;
}

bool MQTTClient::unsubscribe(const char topic[]) {
// return immediately if not connected
if (!this->connected()) {
@@ -459,6 +483,28 @@ bool MQTTClient::unsubscribe(const char topic[]) {
return true;
}

bool MQTTClient::unsubscribeMultiple(int count, const char *topics[]) {
// return immediately if not connected
if (!this->connected()) {
return false;
}

lwmqtt_string_t topic_filters[count];

for (int i = 0; i < count; i++) {
topic_filters[i] = lwmqtt_string(topics[i]);
}

this->_lastError = lwmqtt_unsubscribe(&this->client, count, topic_filters, this->timeout);
if (this->_lastError != LWMQTT_SUCCESS) {
// close connection
this->close();
return false;
}

return true;
}

bool MQTTClient::loop() {
// return immediately if not connected
if (!this->connected()) {
2 changes: 2 additions & 0 deletions src/MQTTClient.h
Original file line number Diff line number Diff line change
@@ -178,9 +178,11 @@ class MQTTClient {
bool subscribe(const String &topic, int qos) { return this->subscribe(topic.c_str(), qos); }
bool subscribe(const char topic[]) { return this->subscribe(topic, 0); }
bool subscribe(const char topic[], int qos);
bool subscribeMultiple(int count, const char *topics[], int qos_levels[]);

bool unsubscribe(const String &topic) { return this->unsubscribe(topic.c_str()); }
bool unsubscribe(const char topic[]);
bool unsubscribeMultiple(int count, const char *topics[]);

bool loop();
bool connected();