-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsub_test.c
More file actions
46 lines (35 loc) · 1.02 KB
/
sub_test.c
File metadata and controls
46 lines (35 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <stdlib.h>
#include <mosquitto.h>
void on_connect(struct mosquitto *mosq, void *obj, int rc) {
printf("ID: %d\n", * (int *) obj);
if(rc) {
printf("Error with result code: %d\n", rc);
exit(-1);
}
mosquitto_subscribe(mosq, NULL, "test/t1", 0);
}
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg) {
printf("New message with topic %s: %s\n", msg->topic, (char *) msg->payload);
}
int main() {
int rc, id=12;
mosquitto_lib_init();
struct mosquitto *mosq;
mosq = mosquitto_new("subscribe-test", true, &id);
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
rc = mosquitto_connect(mosq, "localhost", 1883, 10);
if(rc) {
printf("Could not connect to Broker with return code %d\n", rc);
return -1;
}
mosquitto_loop_start(mosq);
printf("Press Enter to quit...\n");
getchar();
mosquitto_loop_stop(mosq, true);
mosquitto_disconnect(mosq);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}