MQTT-3.1.1-CN 1
MQTT 协议 3.1.1 中文版
OASIS 标准
2014 10 29
规范链接
当前版本
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.doc (Authoritative)
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.pdf
以前的版本:
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/cos01/mqtt-v3.1.1-cos01.doc (Authoritative)
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/cos01/mqtt-v3.1.1-cos01.html
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/cos01/mqtt-v3.1.1-cos01.pdf
最新版本
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.doc (Authoritative)
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.pdf
技术委员会:
结构化信息标准促进组织 MQTT 技术委员会
主席
拉斐尔·J·科恩 (raphael.cohn@stormmq.com), 个人
理查德·J·科彭 (coppen@uk.ibm.com), IBM
编辑
安德鲁·班克斯 (Andrew_Banks@uk.ibm.com), IBM
拉胡尔·吉普塔 (rahul.gupta@us.ibm.com), IBM
相关文档
本规范与此有关
MQTT NIST 网络安全框架 1.0 版。 编辑是杰夫·布朗和路易·菲利普·拉穆勒。最新版
本: http://docs.oasis-open.org/mqtt/mqtt-nist-cybersecurity/v1.0/mqtt-nist-cybersecurity-
v1.0.html.
摘要
MQTT 是一个客户端服务端架构的发/订阅模式的消息传输协议。它的设计思想是轻巧、开放
简单规范因此易于实现。这些特点使得它对很多场景来说都是很好的选择,包括受限的环境如
机器与机器的通信(M2M以及物联网环境(IoT这些场景要求很小的代码封装或者网络带宽
非常昂贵。
本协议运行 TCP/IP,或其它提供了有序、可靠、双向连接的网络连接上。它有以下特点:
使用发布/订阅消息模式,提供了一对多的消息分发和应用之间的解耦。
消息传输不需要知道负载内容
提供三种等级的服务质量:.
MQTT-3.1.1-CN 2
最多一次,尽操作环境所能提供的最大努力分发消息。消息可能会丢失。例如,这个
等级可用于环境传感器数据,单次的数据丢失没关系,因为不久之后会再次发送。
至少一次,保证消息可以到达,但是可能会重复。
仅一次保证消息只到达一次。例如,这个等级可用在一个计费系统中,这里如果
消息重复或丢失会导致不正确的收费。
很小的传输消耗和协议数据交换,最大限度减少网络流量
异常连接断开发生时,能通知到相关各方。
状态:
本文档最后由 OASIS 成员在上面标示的日期最终修订或批准。批准的级别也在上面列出了。如果
要查看本文档最新的修订版请检查上面的
最新版本
位置。技术委员会产生的其它修订版和其它技
术文档都列在这里:https://www.oasis-
open.org/committees/tc_home.php?wg_abbrev=mqtt#technical
技术委员会成员对本规范的评论应该发送到技术委员会的邮件列表。其他人应该发送评论到技术委
员会的公共评论列表,方法是点击技术委员会网站的 发送评论 按钮,网页地址是
https://www.oasis-open.org/committees/mqtt/
关于实现本规范必不可少的任何专利是否已公开,以及其它的专利许可条款相关的信,请参考技
术委员会网站的知识产权部分(https://www.oasis-open.org/committees/mqtt/ipr.php)。
引用格式:
引用此规范时应该使用下面的引文格式:
[mqtt-v3.1.1]
MQTT Version 3.1.1. Edited by Andrew Banks and Rahul Gupta. 29 October 2014. OASIS
Standard. http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html. Latest version:
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html.
MQTT-3.1.1-CN 3
文档链接
MQTT 协议 3.1.1 中文翻译项目
MQTT 协议 3.1.1 中文版 PDF
修订记录
发布说明
1.0.0
2015-07-30
翻译全部文本,完成初步审校,公开发布第一版
1.0.1
2015-10-22
修订几处笔误,增补几处未翻译的文本
于译者
GitHub
Blog
Email
MQTT-3.1.1-CN 4
目录
1 概述 ...................................................................................................................................................... 8
1.1 MQTT 协议的组织结构 ........................................................................................................................ 8
1.2 术语 ..................................................................................................................................................... 8
1.3 规范引用 .............................................................................................................................................. 9
1.4 非规范引 ........................................................................................................................................ 10
1.5 数据表示 ............................................................................................................................................ 12
1.5.1 二进制位 .................................................................................................................................... 12
1.5.2 整数数值 .................................................................................................................................... 12
1.5.3 UTF-8 编码字符串 ..................................................................................................................... 12
1.6 编辑约定 ............................................................................................................................................ 13
2 MQTT 控制报文格式 ........................................................................................................................... 14
2.1 MQTT 控制报文的结构 ...................................................................................................................... 14
2.2 固定报头 ............................................................................................................................................ 14
2.2.1 MQTT 控制报文的类型 .............................................................................................................. 14
2.2.2 标志 ........................................................................................................................................... 15
2.2.3 剩余长度 .................................................................................................................................... 16
2.3 可变报头 ............................................................................................................................................ 17
2.3.1 报文标识符 ................................................................................................................................. 17
2.4 有效载荷 ............................................................................................................................................ 19
3 MQTT 控制报 .................................................................................................................................. 20
3.1 CONNECT 连接服务端 .................................................................................................................. 20
3.1.1 固定报头 .................................................................................................................................... 20
3.1.2 可变报头 .................................................................................................................................... 20
3.1.3 有效载荷 .................................................................................................................................... 26
3.1.4 响应 ........................................................................................................................................... 27
3.2 CONNACK 认连接请 .............................................................................................................. 28
3.2.1 固定报头 .................................................................................................................................... 28
3.2.2 可变报头 .................................................................................................................................... 28
3.2.3 有效载荷 .................................................................................................................................... 30
3.3 PUBLISH 发布消息 ........................................................................................................................ 30
3.3.1 固定报头 .................................................................................................................................... 30
3.3.2 可变报头 .................................................................................................................................... 32
3.3.3 有效载荷 .................................................................................................................................... 33
3.3.4 响应 ........................................................................................................................................... 33
3.3.5 动作 ........................................................................................................................................... 33
3.4 PUBACK 发布确认 .......................................................................................................................... 33
3.4.1 固定报头 .................................................................................................................................... 33
3.4.2 可变报头 .................................................................................................................................... 34
3.4.3 有效载荷 .................................................................................................................................... 34
MQTT-3.1.1-CN 5
3.4.4 动作 ........................................................................................................................................... 34
3.5 PUBREC 布收到(QoS 2,第一步) ........................................................................................ 34
3.5.1 固定报头 .................................................................................................................................... 34
3.5.2 可变报头 .................................................................................................................................... 34
3.5.3 有效载荷 .................................................................................................................................... 35
3.5.4 动作 ........................................................................................................................................... 35
3.6 PUBREL 发布释放(QoS 2,第二步) ......................................................................................... 35
3.6.1 固定报头 .................................................................................................................................... 35
3.6.2 可变报头 .................................................................................................................................... 35
3.6.3 有效载荷 .................................................................................................................................... 36
3.6.4 动作 ........................................................................................................................................... 36
3.7 PUBCOMP 布完成(QoS 2,第三步) ..................................................................................... 36
3.7.1 固定报头 .................................................................................................................................... 36
3.7.2 可变报头 .................................................................................................................................... 36
3.7.3 有效载荷 .................................................................................................................................... 36
3.7.4 动作 ........................................................................................................................................... 36
3.8 SUBSCRIBE - 订阅主题 ................................................................................................................... 37
3.8.1 固定报头 .................................................................................................................................... 37
3.8.2 可变报头 .................................................................................................................................... 37
3.8.3 有效载荷 .................................................................................................................................... 37
3.8.4 响应 ........................................................................................................................................... 39
3.9 SUBACK 订阅确认 ......................................................................................................................... 40
3.9.1 固定报头 .................................................................................................................................... 40
3.9.2 可变报头 .................................................................................................................................... 40
3.9.3 有效载荷 .................................................................................................................................... 41
3.10 UNSUBSCRIBE 取消订阅 ............................................................................................................ 41
3.10.1 固定报头 .................................................................................................................................. 42
3.10.2 可变报头 .................................................................................................................................. 42
3.10.3 有效载荷 .................................................................................................................................. 42
3.10.4 响应 ......................................................................................................................................... 43
3.11 UNSUBACK 取消订阅确认 .......................................................................................................... 43
3.11.1 固定报头 .................................................................................................................................. 44
3.11.2 可变报头 .................................................................................................................................. 44
3.11.3 有效载荷 .................................................................................................................................. 44
3.12 PINGREQ 心跳请求 ..................................................................................................................... 44
3.12.1 固定报头 .................................................................................................................................. 44
3.12.2 可变报头 .................................................................................................................................. 45
3.12.3 有效载荷 .................................................................................................................................. 45
3.12.4 响应 ......................................................................................................................................... 45
3.13 PINGRESP 心跳响应 ................................................................................................................... 45
3.13.1 固定报头 .................................................................................................................................. 45
MQTT-3.1.1-CN 6
3.13.2 可变报头 .................................................................................................................................. 45
3.13.3 有效载荷 .................................................................................................................................. 45
3.14 DISCONNECT 断开连接............................................................................................................... 45
3.14.1 固定报头 .................................................................................................................................. 46
3.14.2 可变报头 .................................................................................................................................. 46
3.14.3 有效载荷 .................................................................................................................................. 46
3.14.4 响应 ......................................................................................................................................... 46
4 操作行为 ............................................................................................................................................. 47
4.1 状态存储 ............................................................................................................................................ 47
4.1.1 非规范示例 ................................................................................................................................. 47
4.2 网络连接 ............................................................................................................................................ 47
4.3 服务质量等级和协议流程 .................................................................................................................. 48
4.3.1 QoS 0:最多分发一次 .................................................................................................................. 48
4.3.2 QoS 1: 至少分发一次 ................................................................................................................. 48
4.3.3 QoS 2: 仅分发一次 .................................................................................................................... 49
4.4 消息分发重试 .................................................................................................................................... 51
4.5 消息收到 ............................................................................................................................................ 51
4.6 消息排序 ............................................................................................................................................ 51
4.7 主题名和主题过滤器 ......................................................................................................................... 52
4.7.1 主题通配符 ................................................................................................................................. 52
4.7.2 $头的主题 ........................................................................................................................... 53
4.7.3 主题语义和用法 ......................................................................................................................... 53
4.8 错误处理 ............................................................................................................................................ 54
5 安全 .................................................................................................................................................... 55
5.1 概述 ................................................................................................................................................... 55
5.2 MQTT 解决方案:安全和认证 ........................................................................................................... 55
5.3 轻量级的加密与受限设备 .................................................................................................................. 55
5.4 实现注意事项 .................................................................................................................................... 55
5.4.1 客户端身份验证 ......................................................................................................................... 56
5.4.2 客户端授权 ................................................................................................................................. 56
5.4.3 服务端身份验证 ......................................................................................................................... 56
5.4.4 控制报文和应用消息的完整性 .................................................................................................... 56
5.4.5 控制报文和应用消息的保密性 .................................................................................................... 56
5.4.6 消息传输的不可否认性 .............................................................................................................. 56
5.4.7 检测客户端和服务端的盗用 ....................................................................................................... 57
5.4.8 检测异常行为 ............................................................................................................................. 57
5.4.9 其它的安全注意事 .................................................................................................................. 57
5.4.10 使用 SOCKS 代理 .................................................................................................................... 58
5.4.11 安全配置文件 ........................................................................................................................... 58
6 使用 WebSocket 为网络层 .............................................................................................................. 59
6.1 IANA 注意事项 .................................................................................................................................. 59
MQTT-3.1.1-CN 7
7 一致性 ................................................................................................................................................. 60
7.1 一致性目 ........................................................................................................................................ 60
7.1.1 MQTT 服务端 ............................................................................................................................. 60
7.1.2 MQTT 客户端 ............................................................................................................................. 60
附录 B 强制性规范声明(非规范) ............................................................................................................. 62
MQTT-3.1.1-CN 8
1 概述
1.1 MQTT 议的组织结构
本规范分为七个章节:
第一章 - 介绍
第二章 MQTT 控制报文格式
第三章 MQTT 控制报文
第四章 操作行为
第五章 安全
第六章 使用 WebSocket 作为网络传输层 t
第七章 一致性目标
1.2 术语
本规范中用到的关键字 必须 MUST不能 MUST NOT要求 REQUIRED将会 SHALL不会 SHALL
NOT应该 SHOULD不应该 SHOULD NOT推荐 RECOMMENDED可以 MAY可选 OPTIONAL
都是按照 IETF RFC 2119 [RFC2119] 中的描述解释。
网络连接Network Connection):
MQTT 使用的底层传输协议基础设施
客户端使用它连接服务端。
它提供有序的、可靠的、双向字节流传输。
例子见 4.2 节。
应用消息Application Message
MQTT 协议通过网络传输应用数据。应用消息通过 MQTT 传输时,它们有关联的服务质量(QoS)和主题
Topic)。
客户端Client):
使用 MQTT 的程序或设备。客户端总是通过网络连接到服务端。它可以
发布应用消息给其它相关的客户端。.
订阅请求接受相关的应用消息
取消订阅以移除接受应用消息的请求。
从服务端断开连接。
服务端Server):
一个程序或设备,作为发送消息的客户端和请求订阅的客户端之间的中介。服务端
接受来自客户端的网络连接
接受客户端发布的应用消息
MQTT-3.1.1-CN 9
处理客户端的订阅和取消订阅请求。
转发应用消息给符合条件的客户端订阅
订阅Subscription
订阅包含一个主题过滤器(Topic Filter)和一个最大的服务质量(QoS)等级。订阅与单个会话
Session)关联。会话可以包含多于一个的订阅。会话的每个订阅都有一个不同的主题过滤器。
主题名(Topic Name):
附加在应用消息上的一个标签,服务端已知且与订阅匹配。服务端发送应用消息的一个副本给每一个匹配
的客户端订阅。
主题过滤器(Topic Filter:
订阅中包含的一个表达式,用于表示相关的一个或多个主题。主题过滤器可以使用通配符。
会话Session):
客户端和服务端之间的状态交互。一些会话持续时长与网络连接一样,另一些可以在客户端和服务端的多
个连续网络连接间扩展。
控制报文MQTT Control Packet):
通过网络连接发送的信息数据包。MQTT 规范定义了十四种不同类型的控制报文,其中一个(PUBLISH
文)用于传输应用消息。
1.3 规范引用
[RFC2119]
Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, March
1997.
http://www.ietf.org/rfc/rfc2119.txt
[RFC3629]
Yergeau, F., "UTF-8, a transformation format of ISO 10646", STD 63, RFC 3629, November 2003
http://www.ietf.org/rfc/rfc3629.txt
[RFC5246]
Dierks, T. and E. Rescorla, "The Transport Layer Security (TLS) Protocol Version 1.2", RFC 5246, August
2008.
http://www.ietf.org/rfc/rfc5246.txt
[RFC6455]
Fette, I. and A. Melnikov, "The WebSocket Protocol", RFC 6455, December 2011.
http://www.ietf.org/rfc/rfc6455.txt
[Unicode]
The Unicode Consortium. The Unicode Standard.
http://www.unicode.org/versions/latest/
MQTT-3.1.1-CN 10
1.4 非规范引用
[RFC793]
Postel, J. Transmission Control Protocol. STD 7, IETF RFC 793, September 1981.
http://www.ietf.org/rfc/rfc793.txt
[AES]
Advanced Encryption Standard (AES) (FIPS PUB 197).
http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
[DES]
Data Encryption Standard (DES).
http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
[FIPS1402]
Security Requirements for Cryptographic Modules (FIPS PUB 140-2)
http://csrc.nist.gov/publications/fips/fips140-2/fips1402.pdf
[IEEE 802.1AR]
IEEE Standard for Local and metropolitan area networks - Secure Device Identity
http://standards.ieee.org/findstds/standard/802.1AR-2009.html
[ISO29192]
ISO/IEC 29192-1:2012 Information technology -- Security techniques -- Lightweight cryptography -- Part
1: General
http://www.iso.org/iso/home/store/catalogue_tc/catalogue_detail.htm?csnumber=56425
[MQTT NIST]
MQTT supplemental publication, MQTT and the NIST Framework for Improving Critical Infrastructure
Cybersecurity
http://docs.oasis-open.org/mqtt/mqtt-nist-cybersecurity/v1.0/mqtt-nist-cybersecurity-v1.0.html
[MQTTV31]
MQTT V3.1 Protocol Specification.
http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html
[NISTCSF]
Improving Critical Infrastructure Cybersecurity Executive Order 13636
http://www.nist.gov/itl/upload/preliminary-cybersecurity-framework.pdf
[NIST7628]
NISTIR 7628 Guidelines for Smart Grid Cyber Security
http://www.nist.gov/smartgrid/upload/nistir-7628_total.pdf
[NSAB]
NSA Suite B Cryptography
http://www.nsa.gov/ia/programs/suiteb_cryptography/
MQTT-3.1.1-CN 11
[PCIDSS]
PCI-DSS Payment Card Industry Data Security Standard
https://www.pcisecuritystandards.org/security_standards/
[RFC1928]
Leech, M., Ganis, M., Lee, Y., Kuris, R., Koblas, D., and L. Jones, "SOCKS Protocol Version 5", RFC
1928, March 1996.
http://www.ietf.org/rfc/rfc1928.txt
[RFC4511]
Sermersheim, J., Ed., "Lightweight Directory Access Protocol (LDAP): The Protocol", RFC 4511, June
2006.
http://www.ietf.org/rfc/rfc4511.txt
[RFC5077]
Salowey, J., Zhou, H., Eronen, P., and H. Tschofenig, "Transport Layer Security (TLS) Session
Resumption without Server-Side State", RFC 5077, January 2008.
http://www.ietf.org/rfc/rfc5077.txt
[RFC5280]
Cooper, D., Santesson, S., Farrell, S., Boeyen, S., Housley, R., and W. Polk, "Internet X.509 Public Key
Infrastructure Certificate and Certificate Revocation List (CRL) Profile", RFC 5280, May 2008.
http://www.ietf.org/rfc/rfc5280.txt
[RFC6066]
Eastlake 3rd, D., "Transport Layer Security (TLS) Extensions: Extension Definitions", RFC 6066, January
2011.
http://www.ietf.org/rfc/rfc6066.txt
[RFC6749]
Hardt, D., Ed., "The OAuth 2.0 Authorization Framework", RFC 6749, October 2012.
http://www.ietf.org/rfc/rfc6749.txt
[RFC6960]
Santesson, S., Myers, M., Ankney, R., Malpani, A., Galperin, S., and C. Adams, "X.509 Internet Public
Key Infrastructure Online Certificate Status Protocol - OCSP", RFC 6960, June 2013.
http://www.ietf.org/rfc/rfc6960.txt
[SARBANES]
Sarbanes-Oxley Act of 2002.
http://www.gpo.gov/fdsys/pkg/PLAW-107publ204/html/PLAW-107publ204.htm
[USEUSAFEHARB]
U.S.-EU Safe Harbor
http://export.gov/safeharbor/eu/eg_main_018365.asp
MQTT-3.1.1-