Mosquitto 0.15 开源MQTT v3.1 Broker

        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.hmosquittopp.lib拷贝到工程目录下;
3.将"...\mosquitto"下的 mosquitto.dllmosquittopp.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显示信息如下:

mosquitto_sub.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 secondsThe number of seconds that mosquitto will wait between each time it saves the in-memory database to disk. 
每次保存内存中的数据库到磁盘的间隔时间,秒数单位
bind_address addressListen for incoming network connections on the specified IP address/hostname only.
只在指定的IP地址/主机名监听传入的网络连接
clientid_prefixes prefixIf 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 portListen for incoming network connection on the specified port.
指定的端口监听传入的网络连接
log_dest destinationsSend 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 typesChoose types of messages to log.
选择要记录的消息类型,默认类型error, warning, notice and information
max_connections countLimit the total number of clients connected for the current listener.
限制当前监听连接的客户端总数,默认-1不限制
max_inflight_messages countThe maximum number of QoS 1 or 2 messages that can be in the process of being transmitted simultaneously.
max_queued_messages countThe maximum number of QoS 1 or 2 messages to hold in the queue above those messages that are currently in flight.
mount_point topic prefixThis option is used with the listener option to isolate groups of clients.
password_file file pathSet 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 nameThe filename to use for the persistent database. Defaults to mosquitto.db.
使用的持久性数据库的文件名
persistence_location pathThe path where the persistence database should be stored.
存放在持久性数据库的路径
pid_file file pathWrite a pid file to the file specified.
port port numberSet 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 secondsThe 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 secondsThe integer number of seconds between the internal message store being cleaned of messages that are no longer referenced. 
sys_interval secondsThe integer number of seconds between updates of the $SYS subscription hierarchy, which provides status information about the broker. 
user usernameWhen 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 idSet the client id for this bridge connection.
connection nameThis variable marks the start of a new bridge connection.
keepalive_interval secondsSet the number of seconds after which the bridge should send a ping if no other traffic has occurred. 
idle_timeout secondsSet 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 valueConfigure a password for the bridge.
start_type [ automatic | lazy | once ]Set the start type of the bridge.
threshold countSet 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 nameConfigure a username for the bridge.
定义与外部数据库的潜在用途的选项

EXTERNAL SECURITY CHECKS

 
db_host hostnameDatabase host name.
db_port portDatabase port.
db_name nameDatabase name.
db_username usernameDatabase username.
db_password passwordDatabase password.
以上只摘官方文档的一部分内容,方便进行大概的查阅,详细的在官方文档上 http://mosquitto.org/documentation/

2.自己编译的C++版静态库 http://download.csdn.net/detail/akof1314/4254918

评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值