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.示例代碼如下:
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(); } 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 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. |
以上隻摘官方文檔的一部分内容,友善進行大概的查閱,詳細的在官方文檔上 http://mosquitto.org/documentation/