Nitpick for MQTT example

PUBLISHED_FROM=a0dd26c00b41363f1cc58c28f530fbdf272ed146
This commit is contained in:
Sergey Lyubka 2017-07-14 13:47:42 +03:00 committed by Cesanta Bot
parent 4c37e49274
commit 0bb14abbdd
3 changed files with 17 additions and 11 deletions

View File

@ -1,4 +1,4 @@
PROG = mqtt_broker PROG = mqtt_broker
MODULE_CFLAGS = -DMG_ENABLE_MQTT_BROKER -DMG_ENABLE_HTTP=0 MODULE_CFLAGS = -DMG_ENABLE_MQTT_BROKER -DMG_ENABLE_HTTP=0
SSL_LIB=openssl #SSL_LIB=openssl
include ../examples.mk include ../examples.mk

View File

@ -17,22 +17,31 @@
#include "../../mongoose.h" #include "../../mongoose.h"
static const char *s_listening_address = "0.0.0.0:1883";
static void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev != MG_EV_POLL) printf("USER HANDLER GOT EVENT %d\n", ev);
/* Do your custom event processing here */
mg_mqtt_broker(c, ev, ev_data);
}
int main(void) { int main(void) {
struct mg_mgr mgr; struct mg_mgr mgr;
const char *address = "0.0.0.0:1883"; struct mg_connection *c;
struct mg_connection *nc;
struct mg_mqtt_broker brk; struct mg_mqtt_broker brk;
mg_mgr_init(&mgr, NULL); mg_mgr_init(&mgr, NULL);
mg_mqtt_broker_init(&brk, NULL); mg_mqtt_broker_init(&brk, NULL);
if ((nc = mg_bind(&mgr, address, mg_mqtt_broker)) == NULL) { if ((c = mg_bind(&mgr, s_listening_address, ev_handler)) == NULL) {
fprintf(stderr, "mg_bind(%s) failed\n", address); fprintf(stderr, "mg_bind(%s) failed\n", s_listening_address);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
nc->user_data = &brk; mg_mqtt_broker_init(&brk, NULL);
c->user_data = &brk;
mg_set_protocol_mqtt(c);
printf("MQTT broker started on %s\n", address); printf("MQTT broker started on %s\n", s_listening_address);
/* /*
* TODO: Add a HTTP status page that shows current sessions * TODO: Add a HTTP status page that shows current sessions

View File

@ -27,10 +27,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p) {
struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p; struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p;
(void) nc; (void) nc;
#if 0 if (ev != MG_EV_POLL) printf("USER HANDLER GOT EVENT %d\n", ev);
if (ev != MG_EV_POLL)
printf("USER HANDLER GOT %d\n", ev);
#endif
switch (ev) { switch (ev) {
case MG_EV_CONNECT: { case MG_EV_CONNECT: {