-
-
Save zinahe/144b95d57d33746013c81f6c22a7d8e2 to your computer and use it in GitHub Desktop.
mosquitto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <mosquitto.h> | |
void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) | |
{ | |
if(message->payloadlen){ | |
printf("%s %s\n", message->topic, message->payload); | |
}else{ | |
printf("%s (null)\n", message->topic); | |
} | |
fflush(stdout); | |
} | |
void my_connect_callback(struct mosquitto *mosq, void *obj, int result) | |
{ | |
int i; | |
if(!result){ | |
/* Subscribe to broker information topics on successful connect. */ | |
mosquitto_subscribe(mosq, NULL, "$SYS/#", 2); | |
}else{ | |
fprintf(stderr, "Connect failed\n"); | |
} | |
} | |
void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos) | |
{ | |
int i; | |
printf("Subscribed (mid: %d): %d", mid, granted_qos[0]); | |
for(i=1; i<qos_count; i++){ | |
printf(", %d", granted_qos[i]); | |
} | |
printf("\n"); | |
} | |
void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str) | |
{ | |
/* Pring all log messages regardless of level. */ | |
printf("%s\n", str); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
char id[30]; | |
int i; | |
char *host = "localhost"; | |
int port = 1883; | |
int keepalive = 60; | |
bool clean_session = true; | |
struct mosquitto *mosq = NULL; | |
mosquitto_lib_init(); | |
mosq = mosquitto_new(id, clean_session, NULL); | |
if(!mosq){ | |
fprintf(stderr, "Error: Out of memory.\n"); | |
return 1; | |
} | |
mosquitto_log_callback_set(mosq, my_log_callback); | |
mosquitto_connect_callback_set(mosq, my_connect_callback); | |
mosquitto_message_callback_set(mosq, my_message_callback); | |
mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); | |
if(mosquitto_connect(mosq, host, port, keepalive)){ | |
fprintf(stderr, "Unable to connect.\n"); | |
return 1; | |
} | |
while(!mosquitto_loop(mosq, -1)){ | |
} | |
mosquitto_destroy(mosq); | |
mosquitto_lib_cleanup(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment