02-unsubscribe-v5.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <mosquitto.h>
  5. static int run = -1;
  6. void on_connect(struct mosquitto *mosq, void *obj, int rc)
  7. {
  8. if(rc){
  9. exit(1);
  10. }else{
  11. mosquitto_unsubscribe(mosq, NULL, "unsubscribe/test");
  12. }
  13. }
  14. void on_disconnect(struct mosquitto *mosq, void *obj, int rc)
  15. {
  16. run = rc;
  17. }
  18. void on_unsubscribe(struct mosquitto *mosq, void *obj, int mid)
  19. {
  20. mosquitto_disconnect(mosq);
  21. }
  22. int main(int argc, char *argv[])
  23. {
  24. int rc;
  25. struct mosquitto *mosq;
  26. int port = atoi(argv[1]);
  27. mosquitto_lib_init();
  28. mosq = mosquitto_new("unsubscribe-test", true, NULL);
  29. if(mosq == NULL){
  30. return 1;
  31. }
  32. mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
  33. mosquitto_connect_callback_set(mosq, on_connect);
  34. mosquitto_disconnect_callback_set(mosq, on_disconnect);
  35. mosquitto_unsubscribe_callback_set(mosq, on_unsubscribe);
  36. rc = mosquitto_connect(mosq, "localhost", port, 60);
  37. while(run == -1){
  38. mosquitto_loop(mosq, -1, 1);
  39. }
  40. mosquitto_destroy(mosq);
  41. mosquitto_lib_cleanup();
  42. return run;
  43. }