当前位置:   article > 正文

MQTT简介之八--mosquitto使用详解_mosquitto 使用

mosquitto 使用

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

mosquitto_pub(发布)的用法

执行mosquitto_pub --help

  1. mosquitto_pub --help
  2. mosquitto_pub is a simple mqtt client that will publish a message on a single topic and exit.
  3. mosquitto_pub version 1.4.8 running on libmosquitto 1.4.8.
  4. Usage: mosquitto_pub [-h host] [-k keepalive] [-p port] [-q qos] [-r] {-f file | -l | -n | -m message} -t topic
  5. [-A bind_address] [-S]
  6. [-i id] [-I id_prefix]
  7. [-d] [--quiet]
  8. [-M max_inflight]
  9. [-u username [-P password]]
  10. [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]
  11. [{--cafile file | --capath dir} [--cert file] [--key file]
  12. [--ciphers ciphers] [--insecure]]
  13. [--psk hex-key --psk-identity identity [--ciphers ciphers]]
  14. [--proxy socks-url]
  15. mosquitto_pub --help
  16. -A : bind the outgoing socket to this host/ip address. Use to control which interface
  17. the client communicates over.
  18. -d : enable debug messages.
  19. -f : send the contents of a file as the message.
  20. -h : mqtt host to connect to. Defaults to localhost.
  21. -i : id to use for this client. Defaults to mosquitto_pub_ appended with the process id.
  22. -I : define the client id as id_prefix appended with the process id. Useful for when the
  23. broker is using the clientid_prefixes option.
  24. -k : keep alive in seconds for this client. Defaults to 60.
  25. -l : read messages from stdin, sending a separate message for each line.
  26. -m : message payload to send.
  27. -M : the maximum inflight messages for QoS 1/2..
  28. -n : send a null (zero length) message.
  29. -p : network port to connect to. Defaults to 1883.
  30. -P : provide a password (requires MQTT 3.1 broker)
  31. -q : quality of service level to use for all messages. Defaults to 0.
  32. -r : message should be retained.
  33. -s : read message from stdin, sending the entire input as a message.
  34. -S : use SRV lookups to determine which host to connect to.
  35. -t : mqtt topic to publish to.
  36. -u : provide a username (requires MQTT 3.1 broker)
  37. -V : specify the version of the MQTT protocol to use when connecting.
  38. Can be mqttv31 or mqttv311. Defaults to mqttv31.
  39. --help : display this message.
  40. --quiet : don't print error messages.
  41. --will-payload : payload for the client Will, which is sent by the broker in case of
  42. unexpected disconnection. If not given and will-topic is set, a zero
  43. length message will be sent.
  44. --will-qos : QoS level for the client Will.
  45. --will-retain : if given, make the client Will retained.
  46. --will-topic : the topic on which to publish the client Will.
  47. --cafile : path to a file containing trusted CA certificates to enable encrypted
  48. communication.
  49. --capath : path to a directory containing trusted CA certificates to enable encrypted
  50. communication.
  51. --cert : client certificate for authentication, if required by server.
  52. --key : client private key for authentication, if required by server.
  53. --ciphers : openssl compatible list of TLS ciphers to support.
  54. --tls-version : TLS protocol version, can be one of tlsv1.2 tlsv1.1 or tlsv1.
  55. Defaults to tlsv1.2 if available.
  56. --insecure : do not check that the server certificate hostname matches the remote
  57. hostname. Using this option means that you cannot be sure that the
  58. remote host is the server you wish to connect to and so is insecure.
  59. Do not use this option in a production environment.
  60. --psk : pre-shared-key in hexadecimal (no leading 0x) to enable TLS-PSK mode.
  61. --psk-identity : client identity string for TLS-PSK mode.
  62. --proxy : SOCKS5 proxy URL of the form:
  63. socks5h://[username[:password]@]hostname[:port]
  64. Only "none" and "username" authentication is supported.
  65. See http://mosquitto.org/ for more information.


选项:

-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消息发送到的主题。

mosquitto_sub(订阅)的用法

mosquitto_sub --help

  1. mosquitto_sub --help
  2. mosquitto_sub is a simple mqtt client that will subscribe to a single topic and print all messages it receives.
  3. mosquitto_sub version 1.4.8 running on libmosquitto 1.4.8.
  4. Usage: mosquitto_sub [-c] [-h host] [-k keepalive] [-p port] [-q qos] [-R] -t topic ...
  5. [-C msg_count] [-T filter_out]
  6. [-A bind_address] [-S]
  7. [-i id] [-I id_prefix]
  8. [-d] [-N] [--quiet] [-v]
  9. [-u username [-P password]]
  10. [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]
  11. [{--cafile file | --capath dir} [--cert file] [--key file]
  12. [--ciphers ciphers] [--insecure]]
  13. [--psk hex-key --psk-identity identity [--ciphers ciphers]]
  14. [--proxy socks-url]
  15. mosquitto_sub --help
  16. -A : bind the outgoing socket to this host/ip address. Use to control which interface
  17. the client communicates over.
  18. -c : disable 'clean session' (store subscription and pending messages when client disconnects).
  19. -C : disconnect and exit after receiving the 'msg_count' messages.
  20. -d : enable debug messages.
  21. -h : mqtt host to connect to. Defaults to localhost.
  22. -i : id to use for this client. Defaults to mosquitto_sub_ appended with the process id.
  23. -I : define the client id as id_prefix appended with the process id. Useful for when the
  24. broker is using the clientid_prefixes option.
  25. -k : keep alive in seconds for this client. Defaults to 60.
  26. -N : do not add an end of line character when printing the payload.
  27. -p : network port to connect to. Defaults to 1883.
  28. -P : provide a password (requires MQTT 3.1 broker)
  29. -q : quality of service level to use for the subscription. Defaults to 0.
  30. -R : do not print stale messages (those with retain set).
  31. -S : use SRV lookups to determine which host to connect to.
  32. -t : mqtt topic to subscribe to. May be repeated multiple times.
  33. -T : topic string to filter out of results. May be repeated.
  34. -u : provide a username (requires MQTT 3.1 broker)
  35. -v : print published messages verbosely.
  36. -V : specify the version of the MQTT protocol to use when connecting.
  37. Can be mqttv31 or mqttv311. Defaults to mqttv31.
  38. --help : display this message.
  39. --quiet : don't print error messages.
  40. --will-payload : payload for the client Will, which is sent by the broker in case of
  41. unexpected disconnection. If not given and will-topic is set, a zero
  42. length message will be sent.
  43. --will-qos : QoS level for the client Will.
  44. --will-retain : if given, make the client Will retained.
  45. --will-topic : the topic on which to publish the client Will.
  46. --cafile : path to a file containing trusted CA certificates to enable encrypted
  47. certificate based communication.
  48. --capath : path to a directory containing trusted CA certificates to enable encrypted
  49. communication.
  50. --cert : client certificate for authentication, if required by server.
  51. --key : client private key for authentication, if required by server.
  52. --ciphers : openssl compatible list of TLS ciphers to support.
  53. --tls-version : TLS protocol version, can be one of tlsv1.2 tlsv1.1 or tlsv1.
  54. Defaults to tlsv1.2 if available.
  55. --insecure : do not check that the server certificate hostname matches the remote
  56. hostname. Using this option means that you cannot be sure that the
  57. remote host is the server you wish to connect to and so is insecure.
  58. Do not use this option in a production environment.
  59. --psk : pre-shared-key in hexadecimal (no leading 0x) to enable TLS-PSK mode.
  60. --psk-identity : client identity string for TLS-PSK mode.
  61. --proxy : SOCKS5 proxy URL of the form:
  62. socks5h://[username[:password]@]hostname[:port]
  63. Only "none" and "username" authentication is supported.
  64. See http://mosquitto.org/ for more information.

mosquitto_sub 订阅到主题,接收到消息时打印
选项:

-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消息发送到的主题。

报文种类


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连接。
————————————————
版权声明:本文为CSDN博主「liefyuan」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_28877125/article/details/78330809

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/188653
推荐阅读
相关标签
  

闽ICP备14008679号