From 0bb14abbddf13634eac91257af437ff6ce2cd280 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Fri, 14 Jul 2017 13:47:42 +0300 Subject: [PATCH] Nitpick for MQTT example PUBLISHED_FROM=a0dd26c00b41363f1cc58c28f530fbdf272ed146 --- examples/mqtt_broker/Makefile | 2 +- examples/mqtt_broker/mqtt_broker.c | 21 +++++++++++++++------ examples/mqtt_client/mqtt_client.c | 5 +---- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/examples/mqtt_broker/Makefile b/examples/mqtt_broker/Makefile index f0c17a0e..86f0b09b 100644 --- a/examples/mqtt_broker/Makefile +++ b/examples/mqtt_broker/Makefile @@ -1,4 +1,4 @@ PROG = mqtt_broker MODULE_CFLAGS = -DMG_ENABLE_MQTT_BROKER -DMG_ENABLE_HTTP=0 -SSL_LIB=openssl +#SSL_LIB=openssl include ../examples.mk diff --git a/examples/mqtt_broker/mqtt_broker.c b/examples/mqtt_broker/mqtt_broker.c index 4c02f9b6..4cc5185b 100644 --- a/examples/mqtt_broker/mqtt_broker.c +++ b/examples/mqtt_broker/mqtt_broker.c @@ -17,22 +17,31 @@ #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) { struct mg_mgr mgr; - const char *address = "0.0.0.0:1883"; - struct mg_connection *nc; + struct mg_connection *c; struct mg_mqtt_broker brk; mg_mgr_init(&mgr, NULL); mg_mqtt_broker_init(&brk, NULL); - if ((nc = mg_bind(&mgr, address, mg_mqtt_broker)) == NULL) { - fprintf(stderr, "mg_bind(%s) failed\n", address); + if ((c = mg_bind(&mgr, s_listening_address, ev_handler)) == NULL) { + fprintf(stderr, "mg_bind(%s) failed\n", s_listening_address); 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 diff --git a/examples/mqtt_client/mqtt_client.c b/examples/mqtt_client/mqtt_client.c index da0282bb..20090efe 100644 --- a/examples/mqtt_client/mqtt_client.c +++ b/examples/mqtt_client/mqtt_client.c @@ -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; (void) nc; -#if 0 - if (ev != MG_EV_POLL) - printf("USER HANDLER GOT %d\n", ev); -#endif + if (ev != MG_EV_POLL) printf("USER HANDLER GOT EVENT %d\n", ev); switch (ev) { case MG_EV_CONNECT: {