Created
September 19, 2019 09:34
-
-
Save tillz/5827f0c41dc23fe116257ee870296308 to your computer and use it in GitHub Desktop.
connect_test.c mosquitto threaded - MWE
This file contains 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 <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <mosquitto.h> | |
// compile with | |
// gcc -o connect_test -lmosquitto -pthread connect_test.c | |
// run with | |
// ./connect_test | |
struct mosquitto *global_mosq = NULL; | |
char* hostname = "mqtt.eclipse.org"; | |
uint32_t port = 1883; | |
void _cb_log( struct mosquitto *mosq, void *userdata, int level, const char *str) { | |
printf("[MQTT_%d]: %s\n", level, str); | |
} | |
void on_disconnect( struct mosquitto *mosq, void *userdata, int rc) { | |
printf("[MQTT]: disconnected\n"); | |
} | |
void on_connect(struct mosquitto *mosq, void *userdata, int rc) { | |
printf("[MQTT]: connected\n"); | |
} | |
void main() { | |
int keepalive = 60; | |
mosquitto_lib_init(); | |
printf("mosq_new success: %s\n",(global_mosq = mosquitto_new(NULL, true, NULL)) ? "Yes" : "No"); | |
mosquitto_log_callback_set(global_mosq, _cb_log); | |
mosquitto_disconnect_callback_set(global_mosq, on_disconnect); | |
mosquitto_connect_callback_set(global_mosq, on_connect); | |
printf("connect_async returned: %d\n", mosquitto_connect_async(global_mosq, hostname, port, keepalive)); | |
printf("loop_start returned: %d\n",mosquitto_loop_start(global_mosq)); | |
//the following line works | |
//mosquitto_loop_forever(global_mosq,-1,1); | |
while(1){sleep(1);} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment