win7安装使用mosquitto,vs2010中使用libmosquittopp
在网络上查找了许多关于安装Mosquitto在Windows系统上的教程后发现都建议先安装Cygwin环境对此感到疑惑是因为我想直接使用Mosquitto而没有选择先安装Cygwin为此决定直接下载了Mosquitto 1.4.10的Windows安装文件并按照以下步骤进行了操作:首先解压获取软件然后运行安装程序完成设置最后启动 Mosquitto服务并验证其正常运行
1 下载、安装、使用
1.1 下载
mosquitto最新版下载:http://mosquitto.org/download/
windows安装选择:
mosquitto-1.4.10-install-win32.exe (~200 kB) (Native build, Windows Vista and up, built with Visual Studio Community 2013)
mosquitto-1.4.10-install-cygwin.exe (~200 kB) (Cygwin build, Windows XP and up)
两者区别已经说明了,只是编译方式不同,第二个应该是多了cygwin相关的库,我两个都试了,都可以,真正要注意的问题是,openssl、pthreads相关的动态库,下面会提到
(源码编译的话,需要下载源码:http://mosquitto.org/files/source/)
1.2 安装
双击“mosquitto-1.4.10-install-win32.exe”,按提示下一步安装好即可
1.3 使用
进入mosquitto安装目录后,在程序启动界面双击“mosquitto.exe”程序图标后会出现启动界面:
1)首次运行程序发现缺少ssleay32.dll文件
解决方法:打开控制面板中的“程序”→“管理程序和功能”,找到并删除相关组件后重新添加安装64位版本的SSLEAY32.DLL、LIBEAY32.DLL以及PThreadVC2.DLL文件到系统路径中
2)运行程序出现闪退现象
解决方法:通过右键调出服务管理器,在服务列表中找到 Mosquitto Broker服务项将其设置为手动启动状态即可解决
完成上述设置后再次启动软件即可实现本地MQTT代理功能
3 发布订阅测试
3.1 用提供的mosquitto.exe、mosquitto_sub.exe、mosquitto_pub.exe测试
3.1.1启动mosquitto服务
打开安装目录,请依次双击'mosquitto.exe'即可。由于启'mosquitto'服务已设置,请不要关闭此窗口,并确保测试过程中始终运行该服务。
3.1.2 订阅
按下Win+R键组合,在启动了命令行界面后切换至mosquitto目录,并在其内输入 Mosquitto_sub命令。其中,“-v”参数用于打印更多调试信息,“-t”则用于指定主题名称,默认情况下MqttTest将被设为目标主题名。
3.1.3 发布
按下Windows快捷键组合键Win+R后,在任务栏中输入CMD
3.2 VS2010项目中测试
构建一个基于Windows 32位的操作系统环境下的控制台应用程序;
将 mosquitto 安装目录下的 dev 文件夹中的 mosquitto.h、mosquittopp.h 和 mosquittopp.lib 文件复制至项目目录中(建议创建专用文件夹存放相关代码);
对 Mosquitto 应用协议栈源码库 mosquittopp.h 文件进行修改;
#include <mosquitto.h>
改成:
#include "mosquitto.h"
- 代码
#include "stdafx.h"
#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 _tmain(int argc, _TCHAR* 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;
}
编译上述代码后无需担心问题。接下来,请将 mosquitto 安装目录下的 mosquitto.dll、mosquittopp.dll 以及 ssleay32.dll、libeay32.dll 和 pthreadVC2.dll 这五个 DLL 文件一并复制到工程输出目录下(即 exe 所在的 directories)。这些 DLL 文件是 Mosquitto 搭载所需的组件,请确保已正确配置环境变量以支持它们的功能。
- 执行 上述代码属于发布主题的测试(对应注释中的内容);建议在运行前先通过命令行启动订阅主题;运行代码后可接收订阅主题的消息。
libmosquitto 和 libmosquittocpp 的功能说明: http://mosquitto.org/api/files/mosquitto-h.html
