分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
Mosquitto是一个开源(BSD许可证)的消息代理,实现MQTT(消息队列遥测传输)协议版本3.1。
1.从
http://mosquitto.org/download/下载Cygwin版的Windows安装包,当前版本为0.15;
2.在安装过程中,可以选择作为系统服务,如下图所示:
3.可在
"服务"中看到此服务,如下图所示:
使用系统服务进行启动的话,安装目录下的配置文件
"mosquitto.conf"
将会被加载使用。
下面简单使用mosquittopp进行发布主题:
1.创建Win32控制台程序,名称:TestMosquitto;
2.将"...\mosquitto\devel"下的
mosquitto.h、mosquittopp.h和
mosquittopp.lib拷贝到工程目录下;
3.将"...\mosquitto"下的
mosquitto.dll和
mosquittopp.dll拷贝到工程输出目录下;
4.修改
mosquittopp.h文件第45行,修改如下:
1 | #include "mosquitto.h" |
5.示例代码如下:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include "stdafx.h" #include <iostream> #include "mosquittopp.h" #pragma comment(lib, "mosquittopp.lib") class mqtt_test:public mosquittopp::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(uint16_t mid) {std::cout<<"on_publish"<<std::endl;} }; int _tmain(int argc, _TCHAR* argv[]) { mosquittopp::mosquittopp::lib_init(); int rc; char buf[1024] = "This is test"; mqtt_test test("testID"); rc = test.connect("127.0.0.1"); if (MOSQ_ERR_SUCCESS == rc) { rc = test.loop(); if (MOSQ_ERR_SUCCESS == rc) { rc = test.publish(NULL, "topic/test", strlen(buf), (uint8_t *)buf); rc = test.loop(); } rc = test.disconnect(); rc = test.loop(); } mosquittopp::mosquittopp::lib_cleanup(); return 0; } |
6.结果如下:
开启
mosquitto.exe程序,开启自带的
mosquitto_sub.exe订阅主题,运行
测试程序,结果如下:
mosquitto.exe显示信息如下:
扫描二维码关注公众号,回复:
4049596 查看本文章
附录:
1.配置选项:
一般配置选项
EXTERNAL SECURITY CHECKS |
|
db_host hostname | Database host name. |
db_port port | Database port. |
db_name name | Database name. |
db_username username | Database username. |
db_password password | Database password. |
2.自己编译的C++版静态库 http://download.csdn.net/detail/akof1314/4254918