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显示信息如下:
附录:
1.配置选项:
一般配置选项
VARIABLES | |
acl_file file path | Set the path to an access control list file |
allow_anonymous [ true | false ] | Boolean value that determines whether clients that connect without providing a username are allowed to connect. 是否允许连接客户无需提供用户名,默认允许 |
autosave_interval seconds | The number of seconds that mosquitto will wait between each time it saves the in-memory database to disk. 每次保存内存中的数据库到磁盘的间隔时间,秒数单位 |
bind_address address | Listen for incoming network connections on the specified IP address/hostname only. 只在指定的IP地址/主机名监听传入的网络连接 |
clientid_prefixes prefix | If defined, only clients that have a clientid with a prefix that matches clientid_prefixes will be allowed to connect to the broker. 如果定义,只有客户端ClientID前缀匹配clientid_prefixes的才被允许连接到broker,默认所有客户端可连 |
connection_messages < true | false > | If set to true, the log will include entries when clients connect and disconnect. 如果设置为true,将包括当客户端连接和断开的日志条目 |
listener port | Listen for incoming network connection on the specified port. 指定的端口监听传入的网络连接 |
log_dest destinations | Send log messages to a particular destination. 发送消息记录到一个特定的目的地 |
log_timestamp [ true | false ] | Boolean value, if set to true a timestamp value will be added to each log entry. 布尔值,如果设置为true的时间戳值将被添加到每个日志条目,默认开启 |
log_type types | Choose types of messages to log. 选择要记录的消息类型,默认类型error, warning, notice and information |
max_connections count | Limit the total number of clients connected for the current listener. 限制当前监听连接的客户端总数,默认-1不限制 |
max_inflight_messages count | The maximum number of QoS 1 or 2 messages that can be in the process of being transmitted simultaneously. |
max_queued_messages count | The maximum number of QoS 1 or 2 messages to hold in the queue above those messages that are currently in flight. |
mount_point topic prefix | This option is used with the listener option to isolate groups of clients. |
password_file file path | Set the path to a password file. 设置密码文件的路径 |
persistence [ true | false ] | If true, connection, subscription and message data will be written to the disk in mosquitto.db at the location dictated by persistence_location. |
persistence_file file name | The filename to use for the persistent database. Defaults to mosquitto.db. 使用的持久性数据库的文件名 |
persistence_location path | The path where the persistence database should be stored. 存放在持久性数据库的路径 |
pid_file file path | Write a pid file to the file specified. |
port port number | Set the network port for the default listener to listen on. Defaults to 1883. 设置为默认监听的网络端口上监听,默认1883 |
retained_persistence [ true | false ] | This is a synonym of the persistence option. 这是一个persistence 选项的代名词 |
retry_interval seconds | The integer number of seconds after a QoS=1 or QoS=2 message has been sent that mosquitto will wait before retrying when no response is received. |
store_clean_interval seconds | The integer number of seconds between the internal message store being cleaned of messages that are no longer referenced. |
sys_interval seconds | The integer number of seconds between updates of the $SYS subscription hierarchy, which provides status information about the broker. |
user username | When run as root, change to this user and its primary group on startup. |
CONFIGURING BRIDGES | |
address address[:port], addresses address[:port] | Specify the address and optionally the port of the bridge to connect to. |
cleansession [ true | false ] | Set the clean session option for this bridge. |
clientid id | Set the client id for this bridge connection. |
connection name | This variable marks the start of a new bridge connection. |
keepalive_interval seconds | Set the number of seconds after which the bridge should send a ping if no other traffic has occurred. |
idle_timeout seconds | Set the amount of time a bridge using the lazy start type must be idle before it will be stopped. |
notifications [ true | false ] | If set to true, publish notification messages to the local and remote brokers giving information about the state of the bridge connection. |
password value | Configure a password for the bridge. |
start_type [ automatic | lazy | once ] | Set the start type of the bridge. |
threshold count | Set the number of messages that need to be queued for a bridge with lazy start type to be restarted. |
topic pattern [ out | in | both ] | Define a topic pattern to be shared between the two brokers. |
username name | Configure a username for the bridge. |
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