MQTT协议使用mosquitto服务器在windows下使用libmosquitto订阅发布,简单小例子

 

1.去官网下载mosquitto(我使用Binary Installation--windows--mosquitto-1.4.14-install-win32.exe):

  

  下载地址:https://mosquitto.org/download/

2.双击安装。

3.使用:

  进入mosquitto安装目录,双击“mosquitto.exe”开启服务。

  如果出现缺少 .dll 文件,去www.zhaodll.com下载相应的dll文件。

  ps:(我在打开mosquitto_sub.exe、mosquitto_pub.exe时缺少pthreadVC2.dll。下载之后放到mosquitto安装目录下即可。)

  

  进入安装目录,双击双击“mosquitto.exe”即可,因为上面已经设置了手动启动mosquitto服务,所以该窗口不要关闭,测试过程重要一直启动该服务

   对于订阅:

  win+R键,输入cmd,即打开命令行,进入mosquitto目录,输入:mosquitto_sub -v -t MqttTest 
  -v表示打印更多调试信息,-t表示指定主题,MqttTest即为主题名

  对于发布:

  win+R键,输入cmd,即打开命令行,进入mosquitto目录,输入: mosquitto_pub -t MqttTest -m HelloMqtt 
  -t表示指定主题,MqttTest即为主题名, 
  -m表示指定消息内容,HelloMqtt即为消息内容

以上测试可用

==================================================================================================================

4.在vs下使用(我用的是vs2015)

1. 创建Win32控制台程序; 
2. 将mosquitto安装目录下的devel文件夹中的mosquitto.h、mosquittopp.h、mosquittopp.lib拷贝到工程目录下(自己创建的cpp文件所在的位置)。
3. 修改mosquittopp.h文件:

#include <mosquitto.h>

  尖括号改为冒号(如下)

#include "mosquitto.h"

  创建你自己的cpp文件,填入下面的代码:

#include <string>
#include <iostream> 
#include "mosquitto.h"
#include "mosquittopp.h" 
#pragma comment(lib, "mosquittopp.lib")
#include "stdafx.h" 
class mqtt_test:public mosqpp::mosquittopp 
{ 
public: 
    mqtt_test(const char *id):mosquittopp(id){} 
    void on_connect(int rc) {std::cout<<"on_connect"<<std::endl;} 
    void on_disconnect() {std::cout<<"on_disconnect"<<std::endl;} 
    void on_publish(int mid) {std::cout<<"on_publish"<<std::endl;} 
    void on_subscribe(int mid, int qos_count, const int *granted_qos);//订阅回调函数
    void on_message(const struct mosquitto_message *message);//订阅主题接收到消息
}; 
std::string g_subTopic="subTopic";
void mqtt_test::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
    std::cout<<"订阅 mid: %d "<<mid<<std::endl;
}
void mqtt_test::on_message(const struct mosquitto_message *message) 
{
    bool res=false;
    mosqpp::topic_matches_sub(g_subTopic.c_str(),message->topic,&res);
    if(res)
    {
        std::string strRcv=(char *)message->payload;
        std::cout<<"来自<"<<message->topic<<">的消息:"<<strRcv<<std::endl;
    }
}
int main(int argc, char* argv[]) 
{ 
    mosqpp::lib_init(); 
    mqtt_test test("client6"); 

    int rc; 
    char buf[1024] = "This is test"; 
    test.username_pw_set("wmy","mqtt");
    rc = test.connect("127.0.0.1");//本地IP 
    char err[1024];
    if(rc == MOSQ_ERR_ERRNO)
        std::cout<<"连接错误:"<< mosqpp::strerror(rc)<<std::endl;//连接出错
    else if (MOSQ_ERR_SUCCESS == rc) 
    { 
        //发布测试
        rc = test.loop(); 
        if (MOSQ_ERR_SUCCESS == rc) 
        { 
            rc = test.publish(NULL, "topic/test", strlen(buf), (const void *)buf); 
            rc = test.loop(); 
        } 
       // rc = test.disconnect(); //订阅测试时注释该行
      //  rc = test.loop(); //订阅测试时注释该行

        test.subscribe(NULL,g_subTopic.c_str());//订阅测试取消注释该行
        rc = test.loop_forever();//订阅测试取消注释该行
    }         
    mosqpp::lib_cleanup(); 
    system("pause");
    return 0; 
}

build上述代码,应该没问题,然后将mosquitto安装目录下的mosquitto.dll、mosquittopp.dll以及ssleay32.dll、libeay32.dll、pthreadVC2.dll,一共5个dll,一起拷贝到工程输出目录下,即exe所在目录; 

然后打开mosquitto.exe      (服务)

然后编译运行你自己的cpp程序 (订阅)

再然后用win+R--cmd--输入 mosquitto_pub  -t  subTopic  -m HelloMqtt 。发布消息hellomqtt,主题是subTopic。(发布)

结果如下图:

 

参考  http://www.zhimengzhe.com/windows/123357.html   这篇文章的

关于.lib .dll 的介绍:http://www.cppblog.com/biao/archive/2013/03/14/198416.html

 

posted on 2017-11-06 11:07  low蛋壳  阅读(6603)  评论(0编辑  收藏  举报

导航