Skip to content

Commit 00f7e8c

Browse files
committed
Support wildcards
1 parent 439904e commit 00f7e8c

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

examples/mqtt-server/main.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
5959
LIST_ADD_HEAD(struct sub, &s_subs, sub);
6060
LOG(LL_INFO,
6161
("SUB %p [%.*s]", c->fd, (int) sub->topic.len, sub->topic.ptr));
62+
// Change '+' to '*' for topic matching using mg_globmatch
63+
for (size_t i = 0; i < sub->topic.len; i++) {
64+
if (sub->topic.ptr[i] == '+') ((char *) sub->topic.ptr)[i] = '*';
65+
}
6266
resp[num_topics++] = qos;
6367
}
6468
mg_mqtt_send_header(c, MQTT_CMD_SUBACK, 0, num_topics + 2);
@@ -72,8 +76,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
7276
LOG(LL_INFO, ("PUB %p [%.*s] -> [%.*s]", c->fd, (int) mm->data.len,
7377
mm->data.ptr, (int) mm->topic.len, mm->topic.ptr));
7478
for (struct sub *sub = s_subs; sub != NULL; sub = sub->next) {
75-
if (mg_strcmp(mm->topic, sub->topic) != 0) continue;
76-
mg_mqtt_pub(sub->c, &mm->topic, &mm->data, 1, false);
79+
if (mg_globmatch(sub->topic.ptr, sub->topic.len, mm->topic.ptr,
80+
mm->topic.len)) {
81+
mg_mqtt_pub(sub->c, &mm->topic, &mm->data, 1, false);
82+
}
7783
}
7884
break;
7985
}

0 commit comments

Comments
 (0)