diff --git a/ChangeLog.txt b/ChangeLog.txt index 600e6194..9e2735be 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -30,9 +30,9 @@ Client library: - Outgoing messages with QoS>1 are no longer retried after a timeout period. Messages will be retried when a client reconnects. - DNS-SRV support is now disabled by default. -- Add mosquitto_subscribe_single() and mosquitto_subscribe_multiple(). These - are two helper functions to make retrieving messages from a broker very - straightforward. +- Add mosquitto_subscribe_simple() This is a helper function to make + retrieving messages from a broker very straightforward. Examples of its user + are in examples/subscribe_simple. Client: - Add -x to mosquitto_sub for printing the payload in hexadecimal format. diff --git a/examples/subscribe_simple/Makefile b/examples/subscribe_simple/Makefile new file mode 100644 index 00000000..f42b1364 --- /dev/null +++ b/examples/subscribe_simple/Makefile @@ -0,0 +1,23 @@ +include ../../config.mk + +.PHONY: all + +all : sub_single sub_multiple + +sub_single : single.o + ${CROSS_COMPILE}${CC} $^ -o $@ ../../lib/libmosquitto.so.${SOVERSION} + +sub_multiple : multiple.o + ${CROSS_COMPILE}${CC} $^ -o $@ ../../lib/libmosquitto.so.${SOVERSION} + +single.o : single.c ../../lib/libmosquitto.so.${SOVERSION} + ${CROSS_COMPILE}${CC} -c $< -o $@ -I../../lib ${CFLAGS} + +multiple.o : multiple.c ../../lib/libmosquitto.so.${SOVERSION} + ${CROSS_COMPILE}${CC} -c $< -o $@ -I../../lib ${CFLAGS} + +../../lib/libmosquitto.so.${SOVERSION} : + $(MAKE) -C ../../lib + +clean : + -rm -f *.o sub_single sub_multiple diff --git a/examples/subscribe_simple/multiple.c b/examples/subscribe_simple/multiple.c new file mode 100644 index 00000000..4938a06e --- /dev/null +++ b/examples/subscribe_simple/multiple.c @@ -0,0 +1,39 @@ +#include +#include +#include "mosquitto.h" + +#define COUNT 3 + +int main(int argc, char *argv[]) +{ + int rc; + int i; + struct mosquitto_message *msg; + + mosquitto_lib_init(); + + rc = mosquitto_subscribe_simple( + &msg, COUNT, + "irc/#", 0, true, + "test.mosquitto.org", 1883, + NULL, 60, true, + NULL, NULL, + NULL, NULL); + + if(rc){ + printf("Error: %s\n", mosquitto_strerror(rc)); + mosquitto_lib_cleanup(); + return rc; + } + + for(i=0; i +#include +#include "mosquitto.h" + +int main(int argc, char *argv[]) +{ + int rc; + struct mosquitto_message *msg; + + mosquitto_lib_init(); + + rc = mosquitto_subscribe_simple( + &msg, 1, + "irc/#", 0, true, + "test.mosquitto.org", 1883, + NULL, 60, true, + NULL, NULL, + NULL, NULL); + + if(rc){ + printf("Error: %s\n", mosquitto_strerror(rc)); + mosquitto_lib_cleanup(); + return rc; + } + + printf("%s %s\n", msg->topic, (char *)msg->payload); + mosquitto_message_free(&msg); + + mosquitto_lib_cleanup(); + + return 0; +} +