Add examples for subscribe_simple.

pull/139/head
Roger A. Light 10 years ago
parent 896b4563fb
commit d157e8c603

@ -30,9 +30,9 @@ Client library:
- Outgoing messages with QoS>1 are no longer retried after a timeout period. - Outgoing messages with QoS>1 are no longer retried after a timeout period.
Messages will be retried when a client reconnects. Messages will be retried when a client reconnects.
- DNS-SRV support is now disabled by default. - DNS-SRV support is now disabled by default.
- Add mosquitto_subscribe_single() and mosquitto_subscribe_multiple(). These - Add mosquitto_subscribe_simple() This is a helper function to make
are two helper functions to make retrieving messages from a broker very retrieving messages from a broker very straightforward. Examples of its user
straightforward. are in examples/subscribe_simple.
Client: Client:
- Add -x to mosquitto_sub for printing the payload in hexadecimal format. - Add -x to mosquitto_sub for printing the payload in hexadecimal format.

@ -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

@ -0,0 +1,39 @@
#include <stdlib.h>
#include <stdio.h>
#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<COUNT; i++){
printf("%s %s\n", msg[i].topic, (char *)msg[i].payload);
mosquitto_message_free_contents(&msg[i]);
}
free(msg);
mosquitto_lib_cleanup();
return 0;
}

@ -0,0 +1,33 @@
#include <stdlib.h>
#include <stdio.h>
#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;
}
Loading…
Cancel
Save