windows下使用mosquitto搭建MQTT服务器的方法
首页 > MQTT   作者:皮皮华  2022年4月6日 10:22 星期三  热度:1670°  字号:   评论:0 条
时间:2022-4-6 10:22   热度:1670°  评论:0 条 
这篇文章主要介绍了windows搭建MQTT服务器,首先下载mosquitto在配置,详细配置方法及MQTTX连接服务器的方法,跟随皮皮华一起通过本文学习下吧

一、下载mosquitto

点击下载安装程序 下载地址,我这边选下载win64位的安装包mosquitto-2.0.14-install-windows-x64.exe

二、配置mosquitto

1. 安装路径打开命令行界面

双击安装包,一路默认安装完成后,进入安装目录打开cmd窗口。

2. 设置user及password

执行:mosquitto_passwd.exe -c pwfile.example pph
设置用户名:pph        设置密码:123456

如果allow_anonymous设置为true,则可用任意用户名和密码登录。


3. 配置文件

mosquitto.exe -c mosquitto.conf 
未报错说明配置正常

    配置文件的修改:如果需要TLS则需要配置证书路径,可以同时打开多个端口。

    其中普通监听端口:不需要配置TLS。

    双向认证监听端口:需要配置所有证书路径。Client验证服务器端的证书,Server验证客户端证书。

    单向认证监听端口:需要配置所有证书路径。Client验证服务器端的证书,比较Client本地配置证书和服务器返回的证书。

#MQTT普通监听端口
listener 8536
#允许匿名,否则会报错:Client<unknown> disconnected, not authorised
allow_anonymous true
#通讯协议
protocol mqtt

#MQTT单向认证监听端口
listener 8537
#允许匿名,否则会报错:Client<unknown> disconnected, not authorised
allow_anonymous true
#通讯协议
protocol mqtt
#CA证书路径 必须为绝对路径
cafile C:\Program Files\mosquitto\cert_test\ca.crt
#服务端证书路径
certfile C:\Program Files\mosquitto\cert_test\server.crt
#服务端秘钥路径
keyfile C:\Program Files\mosquitto\cert_test\server.key

#MQTT双向认证监听端口
listener 8538
#允许匿名,否则会报错:Client<unknown> disconnected, not authorised
allow_anonymous true
#通讯协议
protocol mqtt
#CA证书路径 必须为绝对路径
cafile C:\Program Files\mosquitto\cert_test\ca.crt
#服务端证书路径
certfile C:\Program Files\mosquitto\cert_test\server.crt
#服务端秘钥路径
keyfile C:\Program Files\mosquitto\cert_test\server.key
#需要验证证书
require_certificate true
#require_certificate 设置为TRUE时,则以下参数也为TRUE
use_identity_as_username true

    其中use_identity_as_username 取值不同,表示两种情况:
    use_identity_as_username 为true表示可以不验证server端的用户名和密码,此时要开启匿名登录 allow_anonymous
    use_identity_as_username 为false表示需要验证server端的用户名和密码

4. 设置端口

默认端口:1883
启动:mosquitto.exe
指定端口启动:mosquitto.exe -p 10086

通常直接采用配置文件方式,不建议使用-p参数:mosquitto.exe -v -c mosquitto.conf -p 10086

5. 订阅者

mosquitto_sub.exe -d -v -i mqtt_test_sub -u mqtt_user_sub -P mqtt_pwd_sub -p 8537 -t test_topic

6. 发布者

mosquitto_pub.exe -d -i mqtt_test_pub -u mqtt_user_pub -P mqtt_pwd_pub -p 8537 -t test_topic -m hellomqtt

7. 命令行测试


三、SSL证书的制作

这里直接给出我的脚本源码,其中IP要改成自己的本地IP或者外网映射IP:

#!/usr/bin/env bash
# Generate the openssl configuration files.
echo "创建openssl.cnf------------------->"
cat > openssl.cnf << EOF
[req]
distinguished_name = req_distinguished_name
req_extensions = v3_req

[req_distinguished_name]
countryName = CN
countryName_default = CN
stateOrProvinceName = Guangdong
stateOrProvinceName_default = Guangdong
localityName = Shenzhen
localityName_default = Shenzhen
organizationalUnitName  = XGD
organizationalUnitName_default  = XGD
commonName = 0.0.0.0
commonName_default = 0.0.0.0
commonName_max  = 64

[ v3_req ]
# Extensions to add to a certificate request
basicConstraints = CA:TRUE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names

[alt_names]
IP.1 = 120.234.66.170
IP.2 = 10.150.132.202
IP.3 = 127.0.0.1

EOF
echo "创建v3.ext------------------->"
cat > v3.ext << EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage=digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName=@alt_names
[alt_names]
IP.1 = 120.234.66.170
IP.2 = 10.150.132.202
IP.3 = 127.0.0.1

EOF
echo "创建CA 根证书------------------------->"
echo "创建私钥 ca.key"
openssl genrsa -out ca.key 2048
echo "生成根证书签发申请文件"
openssl req -new -sha256 -subj /C=CN/ST=Guangdong/L=Shenzhen/O=XGD/OU=Software/CN=ca/emailAddress=ca@xgd.com -key ca.key -out ca.csr
echo "自签发根证书"
openssl x509 -req -in ca.csr -out ca.crt -signkey ca.key -days 3650
#echo "查看证书内容"
#openssl x509 -in ca.crt -noout -text
echo "生成服务器证书----------------->"
echo "创建私钥 server.key"
openssl genrsa -out server.key 2048
echo "创建服务器证书请求文件 server.csr"
openssl req -new -sha256 -subj /C=CN/ST=Guangdong/L=Shenzhen/O=XGD/OU=Software/CN=server/emailAddress=ca@xgd.com -key server.key -out server.csr -config openssl.cnf
echo "创建服务器证书 server.crt"
openssl x509 -req -CAcreateserial -in server.csr -out server.crt -CA ca.crt -CAkey ca.key -days 3650 -extfile v3.ext
#echo "创建pem------------------------>"
#cat server.crt server.key > server.pem
#echo "创建p12----------------------->"
#openssl pkcs12 -export -in server.crt -inkey server.key -out server.p12 -name "server"
echo "生成客户端证书----------------->"
echo "创建私钥 client.key"
openssl genrsa -out client.key 2048
echo "创建客户端证书请求文件 client.csr"
openssl req -new -sha256 -subj /C=CN/ST=Guangdong/L=Shenzhen/O=XGD/OU=Software/CN=client/emailAddress=ca@xgd.com -key client.key -out client.csr -config openssl.cnf
echo "创建客户端证书 client.crt"
openssl x509 -req -CAcreateserial -in client.csr -out client.crt -CA ca.crt -CAkey ca.key -days 3650 -extfile v3.ext
#echo "创建pem------------------------>"
#cat client.crt client.key > client.pem
#echo "创建p12----------------------->"
#openssl pkcs12 -export -in client.crt -inkey client.key -out client.p12 -name "client"
echo "创建完成"

四、常见的错误

在调试配置文件的过程中出了很多问题,以下是我遇到比较多的问题:

        使用自签名证书时,CA证书 和 server证书 的 Comon Name 使用了相同的内容;这样会导致 OpenSSL 校验证书时失败,将 CA证书 和 server证书的 Comon Name 改成不同的内容即可,参考脚本源码中的CN
        使用自签名证书时,server证书 的 Comon Name 与域名不相符,默认情况下客户端会连接错误,这时在连接时加入 --insecure 参数即可;
        使用自签名证书时,如果CA是单个文件,将 --cafile 参数错写成 --capath;
        使用自签名证书时,一定要配置本地的IP到ssl配置文件cnf中;
        服务端开启双向认证 require_certificate true , 连接时没有传入客户端的证书和密钥,需要在客户端上配置好TSL连接,及相关的证书路径。
        服务端与客户端的 TLS 版本不一致,服务端配置参数为 tls_version ,客户端配置参数为 --tls-version;这是最头痛的问题,因为最开始习惯用MQTTX,比较美观简约,并且发生错误时有比较清晰的原因。但有个致命的缺点,使用wireshak抓包发现协议是TLS1.3,mosquitto会报协议错误。所以需要使用MQTT.fx软件设置TLS版本测试。配置方式见文下。

五、使用MQTT.fx进行SSL连接测试

1. 配置参数

单向认证:

双向认证:

2. 订阅topic

3. 发布消息

六、MQTT.fx的脚本

1. js脚本所在位置

MQTT.fx的高级用法,就是通过js脚本发送和接收云端的消息。当需要连续向设备发送多条消息时,脚本特别好用。

默认的工作目录是C:\Users\Administrator\AppData\Local\MQTT-FX

注意当我们不是超级管理员,而以个人用户开机登录的电脑,比如我的登录名称为pph,则工作目录为C:\Users\pph\AppData\Local\MQTT-FX

脚本文件放在文件夹scripts中,里面已经有一个默认的模板:01__Switch_Fountain_Test.js

我们可以再创建自己的js文件:02__Publish_Test.js等等,当然要重启软件后才能识别到新添加的脚本文件。

执行脚本的方法:打开MQTT.fx > 连接Connect > 选择Scripts > 下拉列表 > 选择脚本 点击Execute运行脚本

3. 日志清理

日志文件mqttfx.log不断在增长,占用空间,必要时清理一下。

3. 脚本源码

//启动一个线程
var Thread = Java.type("java.lang.Thread");
//执行入口函数,可以理解为C语言的main函数
function execute(action) {
    out("Test Script: " + action.getName());
    for (var i = 0; i < 10000; i++) {
        publish("test_Topic");
        Thread.sleep(5000);//睡眠等待5秒
        publish("test_Topic2");
        Thread.sleep(5000);
        publish("test_Topic3");
        Thread.sleep(5000);
        publish("test_Topic4");
        Thread.sleep(5000);
    }
    action.setExitCode(0);
    action.setResultText("done.");
    out("Test Script: Done");
    return action;
}
//发布消息函数
function publish(topic) {
    out("publish " + topic);
    //mqttManager.publish(topic, "I'mPPHpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowapowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjiodawpowajdioawjioytd0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
	mqttManager.publish(topic, "I'mPPH");
}
//打印函数
function out(message){
     output.print(message);
}

4. 测试结果

七、配置文件详解


配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是Mosquitto的配置文件,即mosquitto.conf,以下是详细的配置参数说明。

# Config file for mosquitto
#
# See mosquitto.conf(5) for more information.
#
# Default values are shown, uncomment to change.
#
# Use the # character to indicate a comment, but only if it is the
# very first character on the line.

# =================================================================
# General configuration
# =================================================================
# 客户端心跳的时间间隔,重新发送已经发出去的Qos 为1或者2的消息的等待时间
retry_interval 20

# 系统状态的刷新时间,设置为0表示不刷新
sys_interval 10

#系统资源的回收时间
#清除在内部消息存储里面的未引用的消息的时间。
#较低的值将占用较少的内存,但处理器时间较长,
#越高的值将产生相反的效果。
#设置值为0意味着未引用的消息将以尽可能快的速度处理。
store_clean_interval 10

#服务进程的PID
#pid_file /var/run/mosquitto.pid

# 以什么用户启动 mosquitto,此配置在 windows 下无效,以非 root 运行无效
#user mosquitto

#客户端心跳消息的最大并非数
#当前每个客户端正在传输的Qo1和2消息的最大数量。
#这包括通过握手信息,以及那些正在重试的信息。
#默认为20。设置为0表示无上限。
#设置为1将保证QoS 1 和2的消息按顺序传递
max_inflight_messages 20

#客户端心跳消息缓存队列
#当前正在进行的队列中Qos 1和2条消息的最大数量。默认为100。
#设置到0表示没有上限(不推荐)。同样可参见queue_qos0_messages
max_queued_messages 100

#设置为true,当一个持久客户端被断开连接时,以Qos为0将消息放到队列中。
#这些消息受max_queued_messages限制
queue_qos0_messages false

#此选项设置被代理允许发布的消息的大小。
#超过这个尺寸的消息将不会被代理接受。
#默认值为0,这意味着所有有效的MQTT消息都被接受。
#MQTT的最大有效大小为268435455字节
message_size_limit 0

# 用于设置客户端长连接的过期时间,默认永不过期,必须以h d w m y为单位
#分别代表 小时,天,星期,月,念
#persistent_client_expiration

# 如果客户端订阅了多个重叠的订阅,例如foo/#和foo/+/baz,然后MQTT期望当代理接
#收到一个与两个订阅相匹配的主题的消息时,例如foo/bar/baz,那么客户端应该只接
#收一次消息。为了满足这一要求,mosquitto不断跟踪发送给客户的消息。允许重复的
#消息选项允许禁用此行为,如果您有大量的客户端订阅相同的主题集合,并且非常关注
#最小化内存使用的情况,那么这个选项可能是有用的。如果你事先知道你的客户端永不
#会有重叠的订阅,那么你的客户必须能够正确处理重复的信息,即使在Qo = 2的时候,
#你的客户端也必须能够正确地处理重复的信息
#allow_duplicate_messages false

# =================================================================
# Default listener
# =================================================================

# 服务绑定的IP地址
#bind_address
# 服务绑定的端口号
#port 1883
# 允许的最大连接数,-1表示没有限制
#max_connections -1

# cafile:CA证书文件
# capath:CA证书目录
# certfile:PEM证书文件
# keyfile:PEM密钥文件
#cafile
#capath
#certfile
#keyfile

# 必须提供证书以保证数据安全性
#require_certificate false

# 若require_certificate值为true,use_identity_as_username也必须为true
#use_identity_as_username false

# 启用PSK(Pre-shared-key)支持
#psk_hint

# SSL/TSL加密算法,可以使用“openssl ciphers”命令获取
# as the output of that command.
#ciphers

# =================================================================
# Persistence
# =================================================================

# 消息自动保存的间隔时间
#autosave_interval 1800

# 消息自动保存功能的开关
#autosave_on_changes false

# 持久化功能的开关
persistence true

# 持久化DB文件
#persistence_file mosquitto.db

# 持久化DB文件目录
#persistence_location /var/lib/mosquitto/

# =================================================================
# Logging
# =================================================================

# 4种日志模式:stdout、stderr、syslog、topic
# none 则表示不记日志,此配置可以提升些许性能
log_dest none

# 选择日志的级别(可设置多项)
#log_type error
#log_type warning
#log_type notice
#log_type information

# 是否记录客户端连接信息
#connection_messages true

# 是否记录日志时间
#log_timestamp true

# =================================================================
# Security
# =================================================================

# 客户端ID的前缀限制,可用于保证安全性
#clientid_prefixes

# 允许匿名用户
#allow_anonymous true

# 用户/密码文件,默认格式:username:password
#password_file

# PSK格式密码文件,默认格式:identity:key
#psk_file

# pattern write sensor/%u/data
# ACL权限配置,常用语法如下:
# 用户限制:user <username>
# 话题限制:topic [read|write] <topic>
# 正则限制:pattern write sensor/%u/data
#acl_file

# =================================================================
# Bridges
# =================================================================

# 允许服务之间使用“桥接”模式(可用于分布式部署)
#connection <name>
#address <host>[:<port>]
#topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]

# 设置桥接的客户端ID
#clientid

# 桥接断开时,是否清除远程服务器中的消息
#cleansession false

# 是否发布桥接的状态信息
#notifications true

# 设置桥接模式下,消息将会发布到的话题地址
# $SYS/broker/connection/<clientid>/state
#notification_topic

# 设置桥接的keepalive数值
#keepalive_interval 60

# 桥接模式,目前有三种:automatic、lazy、once
#start_type automatic

# 桥接模式automatic的超时时间
#restart_timeout 30

# 桥接模式lazy的超时时间
#idle_timeout 60

# 桥接客户端的用户名
#username

# 桥接客户端的密码
#password
# bridge_cafile:桥接客户端的CA证书文件
# bridge_capath:桥接客户端的CA证书目录
# bridge_certfile:桥接客户端的PEM证书文件
# bridge_keyfile:桥接客户端的PEM密钥文件
#bridge_cafile
#bridge_capath
#bridge_certfile
#bridge_keyfile


# 自己的配置可以放到以下目录中
include_dir /etc/mosquitto/conf.d


启动Mosquitto服务很简单,直接运行命令行“mosquitto -c /etc/mosquitto/mosquitto.conf -d”即可。另外,Mosquitto是个纯异步IO框架,经测试可以轻松处理20000个以上的客户端连接。当然,实际的最大承载量还和业务的复杂度有比较大的关系。测试的时候不要忘记调整系统的最大连接数和栈大小,比如Linux上可用ulimit -n20000 -s512命令设置你需要的系统参数



八、参考资料


官方原英文资料

https://mosquitto.org/man/mosquitto-conf-5.html


SSL/TLS 双向认证(二) – 基于mosquittto的MQTT双向认证
https://blog.csdn.net/ustccw/article/details/76905459

mqtt mosquitto TLS安全连接,单向和双向认证
https://blog.csdn.net/qq_36202485/article/details/88949091

windows下的mosquitto安装配置
https://blog.csdn.net/qq_22111417/article/details/84142509

windows平台Mosquitto使用
https://blog.csdn.net/sxpsxp12/article/details/77870109

基于mosquitto的MQTT服务器—SSL/TLS 单向认证+双向认证
https://blog.csdn.net/ty1121466568/article/details/81118468

mosquitto 常用命令
https://www.cnblogs.com/smartlife/articles/10182136.html

SSL3_GET_CLIENT_CERTIFICATE:peer did not return a certificate
https://blog.csdn.net/moxiaomomo/article/details/52995447

OpenSSL生成CA证书及终端用户证书
https://www.cnblogs.com/nidey/p/9041960.html

Paho -物联网 MQTT C Cient的实现和详解
https://www.cnblogs.com/homejim/p/8196763.html

 您阅读这篇文章共花了: 
捐赠支持:如果觉得这篇文章对您有帮助,请“扫一扫”鼓励作者!
 本文无需标签!
二维码加载中...
本文作者:皮皮华      文章标题: windows下使用mosquitto搭建MQTT服务器的方法
本文地址:http://huazai.eleuu.com/?post=53
版权声明:若无注明,本文皆为“皮皮华博客”原创,转载请保留文章出处。

发表吐槽

你肿么看?

你还可以输入 250 / 250 个字

嘻嘻 大笑 可怜 吃惊 害羞 调皮 鄙视 示爱 大哭 开心 偷笑 嘘 奸笑 委屈 抱抱 愤怒 思考 日了狗

评论信息框


既然没有吐槽,那就赶紧抢沙发吧!