Move mg_mqtt_next-* to the example, and some nits

This commit is contained in:
cpq committed 2022-08-26 14:20:54 +01:00
1 parent ffa033cca9
commit c7b9ca68c2
10 files changed
+45 -150

No files matched your search

-75
View File
@@ -1799,81 +1799,6 @@ Return value: None
mg_mqtt_sub(c, mg_str("my/topic"), 1);
```
### mg\_mqtt\_next\_sub()
```c
size_t mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic, uint8_t *qos, size_t pos);
```
Traverse list of subscribed topics.
Used to implement MQTT server when `MQTT_CMD_SUBSCRIBE` is received.
Initial position `pos` should be 4.
Parameters:
- `mgs` - MQTT message
- `topic` - Pointer to `mg_str` to receive topic
- `qos` - Pointer to `uint8_t` to receive QoS
- `pos` - Position to list from
Return value: Next position, or 0 when done
Usage example:
```c
// Mongoose events handler
void fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
if (ev == MG_EV_MQTT_CMD) {
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
if (mm->cmd == MQTT_CMD_SUBSCRIBE) {
size_t pos = 4;
uint8_t qos;
struct mg_str topic;
// Iterate over all subscribed topics
while ((pos = mg_mqtt_next_sub(mm, &topic, &qos, pos)) > 0) {
LOG(LL_INFO, ("SUB [%.*s]", (int) topic.len, topic.ptr));
}
}
}
}
```
### mg\_mqtt\_next\_unsub()
```c
size_t mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic, size_t pos);
```
Same as `mg_mqtt_next_sub()`, but for unsubscribed topics. The difference
is that there is no QoS in unsubscribe request.
Parameters:
- `mgs` - MQTT message
- `topic` - Pointer to `mg_str` to receive topic
- `pos` - Position from which to list
Return value: Next position, or 0 when done
Usage example:
```c
// Mongoose events handler
void fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
if (ev == MG_EV_MQTT_CMD) {
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
if (mm->cmd == MQTT_CMD_UNSUBSCRIBE) {
size_t pos = 4;
struct mg_str topic;
if (mm->cmd == MQTT_CMD_UNSUBSCRIBE) {
// Iterate over all unsubscribed topics
while ((pos = mg_mqtt_next_unsub(mm, &topic, pos)) > 0) {
LOG(LL_INFO, ("SUB [%.*s]", (int) topic.len, topic.ptr));
}
}
}
}
}
```
### mg\_mqtt\_send_header()
```c