mqtt之mosquitto使用

mosquitto是一个开源broker,并且支持MQTTv3.1,支持客户端的验证。mosquitto包括服务端和客户端。


[root@localhost ~]# mosquitto -h
mosquitto version 1.5.1
mosquitto is an MQTT v3.1.1 broker.
Usage: mosquitto [-c config_file] [-d] [-h] [-p port]

 -c : specify the broker config file.
 -d : put the broker into the background after starting.
 -h : display this help.
 -p : start the broker listening on the specified port.
      Not recommended in conjunction with the -c option.
 -v : verbose mode - enable all logging types. This overrides
      any logging options given in the config file.

See http://mosquitto.org/ for more information.
[root@localhost ~]# mosquitto_sub --help
mosquitto_sub is a simple mqtt client that will subscribe to a set of topics and print all messages it receives.
mosquitto_sub version 1.5.1 running on libmosquitto 1.5.1.

Usage: mosquitto_sub {[-h host] [-p port] [-u username [-P password]] -t topic | -L URL [-t topic]}
                     [-c] [-k keepalive] [-q qos]
                     [-C msg_count] [-R] [--retained-only] [-T filter_out] [-U topic ...]
                     [-F format]
                     [-W timeout_secs]
                     [-A bind_address]
                     [-i id] [-I id_prefix]
                     [-d] [-N] [--quiet] [-v]
                     [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]
                     [{--cafile file | --capath dir} [--cert file] [--key file]
                      [--ciphers ciphers] [--insecure]]
                     [--psk hex-key --psk-identity identity [--ciphers ciphers]]
                     [--proxy socks-url]
       mosquitto_sub --help

 -A : bind the outgoing socket to this host/ip address. Use to control which interface
      the client communicates over.
 -c : disable 'clean session' (store subscription and pending messages when client disconnects).
 -C : disconnect and exit after receiving the 'msg_count' messages.
 -d : enable debug messages.
 -F : output format.
 -h : mqtt host to connect to. Defaults to localhost.
 -i : id to use for this client. Defaults to mosquitto_sub_ appended with the process id.
 -I : define the client id as id_prefix appended with the process id. Useful for when the
      broker is using the clientid_prefixes option.
 -k : keep alive in seconds for this client. Defaults to 60.
 -L : specify user, password, hostname, port and topic as a URL in the form:
      mqtt(s)://[username[:password]@]host[:port]/topic
 -N : do not add an end of line character when printing the payload.
 -p : network port to connect to. Defaults to 1883 for plain MQTT and 8883 for MQTT over TLS.
 -P : provide a password
 -q : quality of service level to use for the subscription. Defaults to 0.
 -R : do not print stale messages (those with retain set).
 -t : mqtt topic to subscribe to. May be repeated multiple times.
 -T : topic string to filter out of results. May be repeated.
 -u : provide a username
 -U : unsubscribe from a topic. May be repeated.
 -v : print published messages verbosely.
 -V : specify the version of the MQTT protocol to use when connecting.
      Can be mqttv31 or mqttv311. Defaults to mqttv311.
 -W : Specifies a timeout in seconds how long to process incoming MQTT messages.
 --help : display this message.
 --quiet : don't print error messages.
 --retained-only : only handle messages with the retained flag set, and exit when the
                   first non-retained message is received.
 --will-payload : payload for the client Will, which is sent by the broker in case of
                  unexpected disconnection. If not given and will-topic is set, a zero
                  length message will be sent.
 --will-qos : QoS level for the client Will.
 --will-retain : if given, make the client Will retained.
 --will-topic : the topic on which to publish the client Will.
 --cafile : path to a file containing trusted CA certificates to enable encrypted
            certificate based communication.
 --capath : path to a directory containing trusted CA certificates to enable encrypted
            communication.
 --cert : client certificate for authentication, if required by server.
 --key : client private key for authentication, if required by server.
 --ciphers : openssl compatible list of TLS ciphers to support.
 --tls-version : TLS protocol version, can be one of tlsv1.2 tlsv1.1 or tlsv1.
                 Defaults to tlsv1.2 if available.
 --insecure : do not check that the server certificate hostname matches the remote
              hostname. Using this option means that you cannot be sure that the
              remote host is the server you wish to connect to and so is insecure.
              Do not use this option in a production environment.
 --psk : pre-shared-key in hexadecimal (no leading 0x) to enable TLS-PSK mode.
 --psk-identity : client identity string for TLS-PSK mode.
 --proxy : SOCKS5 proxy URL of the form:
           socks5h://[username[:password]@]hostname[:port]
           Only "none" and "username" authentication is supported.

See http://mosquitto.org/ for more information.
[root@localhost ~]# mosquitto_pub --help
mosquitto_pub is a simple mqtt client that will publish a message on a single topic and exit.
mosquitto_pub version 1.5.1 running on libmosquitto 1.5.1.

Usage: mosquitto_pub {[-h host] [-p port] [-u username [-P password]] -t topic | -L URL}
                     {-f file | -l | -n | -m message}
                     [-c] [-k keepalive] [-q qos] [-r]
                     [-A bind_address]
                     [-i id] [-I id_prefix]
                     [-d] [--quiet]
                     [-M max_inflight]
                     [-u username [-P password]]
                     [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]
                     [{--cafile file | --capath dir} [--cert file] [--key file]
                      [--ciphers ciphers] [--insecure]]
                     [--psk hex-key --psk-identity identity [--ciphers ciphers]]
                     [--proxy socks-url]
       mosquitto_pub --help

 -A : bind the outgoing socket to this host/ip address. Use to control which interface
      the client communicates over.
 -d : enable debug messages.
 -f : send the contents of a file as the message.
 -h : mqtt host to connect to. Defaults to localhost.
 -i : id to use for this client. Defaults to mosquitto_pub_ appended with the process id.
 -I : define the client id as id_prefix appended with the process id. Useful for when the
      broker is using the clientid_prefixes option.
 -k : keep alive in seconds for this client. Defaults to 60.
 -L : specify user, password, hostname, port and topic as a URL in the form:
      mqtt(s)://[username[:password]@]host[:port]/topic
 -l : read messages from stdin, sending a separate message for each line.
 -m : message payload to send.
 -M : the maximum inflight messages for QoS 1/2..
 -n : send a null (zero length) message.
 -p : network port to connect to. Defaults to 1883 for plain MQTT and 8883 for MQTT over TLS.
 -P : provide a password
 -q : quality of service level to use for all messages. Defaults to 0.
 -r : message should be retained.
 -s : read message from stdin, sending the entire input as a message.
 -t : mqtt topic to publish to.
 -u : provide a username
 -V : specify the version of the MQTT protocol to use when connecting.
      Can be mqttv31 or mqttv311. Defaults to mqttv311.
 --help : display this message.
 --quiet : don't print error messages.
 --will-payload : payload for the client Will, which is sent by the broker in case of
                  unexpected disconnection. If not given and will-topic is set, a zero
                  length message will be sent.
 --will-qos : QoS level for the client Will.
 --will-retain : if given, make the client Will retained.
 --will-topic : the topic on which to publish the client Will.
 --cafile : path to a file containing trusted CA certificates to enable encrypted
            communication.
 --capath : path to a directory containing trusted CA certificates to enable encrypted
            communication.
 --cert : client certificate for authentication, if required by server.
 --key : client private key for authentication, if required by server.
 --ciphers : openssl compatible list of TLS ciphers to support.
 --tls-version : TLS protocol version, can be one of tlsv1.2 tlsv1.1 or tlsv1.
                 Defaults to tlsv1.2 if available.
 --insecure : do not check that the server certificate hostname matches the remote
              hostname. Using this option means that you cannot be sure that the
              remote host is the server you wish to connect to and so is insecure.
              Do not use this option in a production environment.
 --psk : pre-shared-key in hexadecimal (no leading 0x) to enable TLS-PSK mode.
 --psk-identity : client identity string for TLS-PSK mode.
 --proxy : SOCKS5 proxy URL of the form:
           socks5h://[username[:password]@]hostname[:port]
           Only "none" and "username" authentication is supported.

See http://mosquitto.org/ for more information.
[root@localhost ~]# mosquitto_passwd --help
mosquitto_passwd is a tool for managing password files for mosquitto.

Usage: mosquitto_passwd [-c | -D] passwordfile username
       mosquitto_passwd -b passwordfile username password
       mosquitto_passwd -U passwordfile
 -b : run in batch mode to allow passing passwords on the command line.
 -c : create a new password file. This will overwrite existing files.
 -D : delete the username rather than adding/updating its password.
 -U : update a plain text password file to use hashed passwords.

See http://mosquitto.org/ for more information.

1、mosquitto_sub

command:订阅主题,接收到消息时打印

mosquitto_sub [-c] [-d] [-h hostname] [-i client_id] [-I client id prefix] [-k keepalive time] [-p port number] [-q message QoS] [--quiet] [-v] [ -u username [-Ppassword] ] [ --will-topic topic [--will-payload payload] [--will-qos qos] [--will-retain] ] -t message topic ...  

options:
-c, –disable-clean-session:禁止’clean session’选项,即如果客户端断开连接,这个订阅仍然保留来接收随后到的QoS为1和2的消息,当改客户端重新连接之后,它将接收到已排在队列中的消息。建议使用此选项时,客户端id选项设为–id

-d, –debug:开启debug选项

-h, –host:说明所连接到的域名,默认是localhost

-i, –id:客户端的ID号,如果没有指定,默认是mosquitto_pub_加上客户端的进程id,不能和–id_prefix同时使用。

-I, –id-prefix:指定客户端ID的前缀,与客户端的进程ID连接组成客户端的ID,不能喝–id同时使用。

-k, –keepalive:给代理发送PING命令(目的在于告知代理该客户端连接保持且在正常工作)的间隔时间,默认是60s

-p, –port:说明客户端连接到的端口,默认是1883

-P, –pw:指定密码用于代理认证,使用此选项时必须有有效的用户名。

-q, –qos:指定消息的服务质量,可以为0,1,2,默认是0.

–quiet:如果指定该选项,则不会有任何错误被打印,当然,这排除了无效的用户输入所引起的错误消息。

-t, –topic:指定订阅的消息主题,允许同时订阅到多个主题

-u, –username:指定用户名用于代理认证。

-v, –verbose:冗长地打印收到的消息。若指定该选项,打印消息时前面会打印主题名——“主题 消息内容”,否则,只打印消息内容

–will-payload:如果指定该选项,则万一客户端意外和代理服务器断开,则该消息将被保留在服务端并发送出去,该选项必须同时用–will-topic指定主题。

–will-qos:指定Will的服务质量,默认是0.必须和选项 –will-topic同时使用.

–will-retain:如果指定该选项,则万一客户端意外断开,已被发送的消息将被当做retained消息。必须和选项 –will-topic同时使用.

–will-topic:指定客户端意外断开时,Will消息发送到的主题。

2、mosquitto_pub

command:

mosquitto_pub [-d] [-h hostname] [-i client_id] [-I client id prefix] [-p port number] [-q message QoS] [--quiet] [-r] { -f file | -l | -m message | -n | -s} [-u username [-P password] ] [ --will-topic topic [--will-payload payload] [--will-qos qos] [--will-retain] ] -t message-topic

options:
-d, –debug :开启debug选项

-f, –file:把一个文件的内容做为消息的内容发送。经测试,支持txt文件,不支持doc等其他形式文件。

-h, –host:说明所连接到的域名,默认是localhost

-i, –id:客户端的ID号,如果没有指定,默认是mosquitto_pub_加上客户端的进程id,不能和–id_prefix同时使用。

-I, –id-prefix :指定客户端ID的前缀,与客户端的进程ID连接组成客户端的ID,不能和–id同时使用。

-l, –stdin-line:从总段读取输入发送消息,一行为一条消息,空白行不会被发送。

-m, –message:从命令行发送一条消息,-m后面跟发送的消息内容。

-n, –null-message:发送一条空消息。

-p, –port:连接的端口号,默认是1883.

-P, –pw:指定密码用于代理认证,使用此选项时必须有有效的用户名。

-q, –qos:指定消息的服务质量,可以为0,1,2,默认是0.

–quiet:如果指定该选项,则不会有任何错误被打印,当然,这排除了无效的用户输入所引起的错误消息。

-r, –retain:如果指定该选项,该条消息将被保留做为最后一条收到的消息。下一个订阅消息者将能至少收到该条消息。

-s, –stdin-file:从标准输入接收传输的消息内容,所有输入做为一条消息发送。

-t, –topic:指定消息所发布到哪个主题。

-u, –username:指定用户名用于代理认证。

–will-payload:如果指定该选项,则万一客户端意外和代理服务器断开,则该消息将被保留在服务端并发送出去,该选项必须同时用–will-topic指定主题。

–will-qos:指定Will的服务质量,默认是0.必须和选项 –will-topic同时使用.

–will-retain:如果指定该选项,则万一客户端意外断开,已被发送的消息将被当做retained消息。必须和选项 –will-topic同时使用.

–will-topic:指定客户端意外断开时,Will消息发送到的主题。


3、报文种类

1.连接请求(CONNECT)

当一个从客户端到服务器的TCP/IP套接字连接被建立时,必须用一个连接流来创建一个协议级别的会话。

2.连接请求确认(CONNECTACK)

连接请求确认报文(CONNECTACK)是服务器发给客户端,用以确认客户端的连接请求

3.发布报文(PUBLISH)

客户端发布报文到服务器端,用来提供给有着不同需求的订阅者们。每个发布的报文都有一个主题,这是一个分层的命名空间,他定义了报文来源分类,方便订阅者订阅他们需要的主题。订阅者们可以注册自己的需要的报文类别。

4.发布确认报文(PUBACK)

发布确认报文(PUBACK)是对服务质量级别为1的发布报文的应答。他可以是服务器对发布报文的客户端的报文确认,也可以是报文订阅者对发布报文的服务器的应答。

5.发布确认报文(PUBREC)

PUBREC报文是对服务质量级别为2的发布报文的应答。这是服务质量级别为2的协议流的第二个报文。PUBREC是由服务器端对发布报文的客户端的应答,或者是报文订阅者对发布报文的服务器的应答。

6.发布确认报文(PUBREL)

PUBREL是报文发布者对来自服务器的PUBREC报文的确认,或者是服务器对来自报文订阅者的PUBREC报文的确认。它是服务质量级别为2的协议流的第三个报文。

7.确定发布完成(PUBCOMP)

PUBCOMP报文是服务器对报文发布者的PUBREL报文的应答,或者是报文订阅者对服务器的PUBREL报文的应答。它是服务质量级别为2的协议流的第四个也是最后一个报文。

8.订阅命名的主题(SUBSCRIBE)

订阅报文(SUBSCRIBE)允许一个客户端在服务器上注册一个或多个感兴趣的主题名字。发布给这些主题的报文作为发布报文从服务器端交付给客户端。订阅报文也描述了订阅者想要收到的发布报文的服务质量等级。

9.订阅报文确认(SUBACK)

当服务器收到客户端发来的订阅报文时,将发送订阅报文的确认报文给客户端。一个这样的确认报文包含一列被授予的服务质量等级。被授予的服务质量等级次序和对应的订阅报文中的主题名称的次序相符。

10.退订命名的主题(UNSUBSCRIBE)

退订主题的报文是从客户端发往服务器端,用以退订命名的主题。

11.退订确认(UNSUBACK)

退订确认报文是从服务器发往客户端,用以确认客户端发来的退订请求报文。

12.Ping请求(PINGREQ)

Ping请求报文是从连接的客户端发往服务器端,用来询问服务器端是否还存在。

13.Ping应答(PINGRESP)

Ping应答报文是从服务器端发往Ping请求的客户端,对客户端的Ping请求进行确认。

14.断开通知(DISCONNECT)

断开通知报文是从客户端发往服务器端用来指明将要关闭它的TCP/IP连接,他允许彻底地断开,而非只是下线。如果客户端已经和干净会话标志集联系,那么所有先前关于客户端维护的信息将被丢弃。一个服务器在收到断开报文之后,不能依赖客户端关闭TCP/IP连接。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MQTT是一种轻量级的通讯协议,适用于物联网设备之间的通信。Mosquitto是一款开源的MQTT代理服务器,它提供了可靠稳定的MQTT消息传递服务。为了满足用户的需求,开发人员推出了一款名为MQTT测试工具的软件,它可以用来测试Mosquitto代理服务器的性能和功能。 MQTT测试工具可以支持多种MQTT协议,包括QoS0、QoS1和QoS2。此外,它还支持通过清除session或发送遗嘱消息来测试Mosquitto的断开连接机制。MQTT测试工具可以达到每秒数千条消息的处理速度,可帮助用户提高Mosquitto代理服务器的性能和功能。 通过使用MQTT测试工具可以实现较低的错误率和较高的消息交付率,从而提高设备之间的通信效率和数据传输的质量。此外,它还支持各种操作系统,如Windows、Linux和MacOS等。 总之,MQTT测试工具是一款非常实用的软件,适用于测试MQTT消息传递的各种参数和功能,帮助提高Mosquitto的性能和功能,让设备之间的通信更加高效。 ### 回答2: MQTTMosquitto)是一种轻量级的通信协议,用于基于订阅/发布模型的物联网设备之间的交互。针对MQTT协议,我们需要使用MQTT测试工具,这样我们才能测试MQTT的各种功能。 Mosquitto是一种流行的MQTT实现。这个工具是一个开源项目,它可以运行在多个平台上,从而使我们能够轻松地测试MQTT应用程序。Mosquitto提供了可用的CLI(命令行界面),为开发者们提供了良好的操作性。它还提供了一些基本测试,包括测试客户端的连接效果和性能。此外,它提供了一些高级测试,如消息订阅的特征、多连接质量、最大qos的处理和消息重传等。 Mosquitto支持基于TLS的安全通讯协议,自动重连功能,多SPS(会话)管理以及客户端订阅策略等等。Mosquitto是一个功能强大的MQTT测试工具,为MQTT开发人员提供了一个好的平台,能够测试他们的MQTT应用程序,并在需要时进行调整。 总之,MQTT测试工具Mosquitto是一款非常好的工具,它能够提高MQTT应用程序的可靠性和性能,同时为MQTT开发人员提供了一个好的平台,使他们能够更有效地测试和调整他们的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值