我是靠谱客的博主 雪白时光,最近开发中收集的这篇文章主要介绍使用mosquitto开发MQTT客户端,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1. 下载并安装openSSL, mosquitto 应用程序运行时会用到

http://slproweb.com/products/Win32OpenSSL.html

 

2. 下载并安装mosquitto

http://mosquitto.org/download/

3. 使用

1)在cmd窗口中,启动Mosquito的MQTT服务

C:Program Filesmosquitto>mosquitto

2)在另一个cmd窗口中,启动侦听通道程序

c:Program Filesmosquitto> mosquitto_sub -t mytopic

3)在另一个cmd启动发布程序

c:Program Filesmosquitto>mosquitto_pub -t mytopic -m 111

此时在第2个窗口中会收到111

4. 开发代码sample

以下代码来自

http://mosquitto.org/man/libmosquitto-3.html

 

Examples

#include <stdio.h>
#include <mosquitto.h>
void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
          if(message->payloadlen){
                    printf("%s %sn", message->topic, message->payload);
          }else{
                    printf("%s (null)n", message->topic);
          }
          fflush(stdout);
}

void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
          int i;
          if(!result){
                    /* Subscribe to broker information topics on successful connect. */
                    mosquitto_subscribe(mosq, NULL, "$SYS/#", 2);
          }else{
                    fprintf(stderr, "Connect failedn");
          }
}

void my_subscribe_callback(struct mosquitto *mosq, void *userdata, 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 *userdata, int level, const char *str)
{
          /* Pring all log messages regardless of level. */
          printf("%sn", str);
}

int main(int argc, char *argv[])
{
          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(NULL, 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;
          }
          mosquitto_loop_forever(mosq, -1, 1);
          mosquitto_destroy(mosq);
          mosquitto_lib_cleanup();
          return 0;
}

 

最后

以上就是雪白时光为你收集整理的使用mosquitto开发MQTT客户端的全部内容,希望文章能够帮你解决使用mosquitto开发MQTT客户端所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部