固定报头(Fixed header)
可变报头(Variable header)
报文有效载荷(Payload)
固定报头
Bit 7 6 5 4 3 2 1 0
byte1 MQTT Packet type Flags
byte2… Remaining Length
报文类型
类型名称 类型值 报文说明
CONNECT 1 发起连接
CONNACK 2 连接回执
PUBLISH 3 发布消息
PUBACK 4 发布回执
PUBREC 5 QoS2消息回执
PUBREL 6 QoS2消息释放
PUBCOMP 7 QoS2消息完成
SUBSCRIBE 8 订阅主题
SUBACK 9 订阅回执
UNSUBSCRIBE 10 取消订阅
UNSUBACK 11 取消订阅回执
PINGREQ 12 PING请求
PINGRESP 13 PING响应
DISCONNECT 14 断开连接
PUBLISH发布消息
PUBLISH报文承载客户端与服务器间双向的发布消息。 PUBACK报文用于接收端确认QoS1报文,PUBREC/PUBREL/PUBCOMP报文用于QoS2消息流程。
PINGREQ/PINGRESP心跳
客户端在无报文发送时,按保活周期(KeepAlive)定时向服务端发送PINGREQ心跳报文,服务端响应PINGRESP报文。PINGREQ/PINGRESP报文均2个字节。
MQTT消息QoS
MQTT发布消息QoS保证不是端到端的,是客户端与服务器之间的。订阅者收到MQTT消息的QoS级别,最终取决于发布消息的QoS和主题订阅的QoS。
发布消息的QoS 主题订阅的QoS 接收消息的QoS
0 0 0
0 1 0
0 2 0
1 0 0
1 1 1
1 2 1
2 0 0
2 1 1
2 2 2
Qos0消息发布订阅
Qos1消息发布订阅
Qos2消息发布订阅
ClientID:861694030142473
客户端IP:172.16.6.*
服务器IP:172.16.6.161
一、客户端连接(这里使用的是MQTT 3.1.0版本)
1、客户端完成TCP三次握手之后,还需要发起CONNECT命令,用户名是libaineu2004,密码是12345678,CleanSession==1,KeepAlive==300s
MQ Telemetry Transport Protocol, Connect Command
Header Flags: 0x10 (Connect Command)
0001 .... = Message Type: Connect Command (1)
.... 0... = DUP Flag: Not set
.... .00. = QoS Level: At most once delivery (Fire and Forget) (0)
.... ...0 = Retain: Not set
Msg Len: 53
Protocol Name Length: 6
Protocol Name: MQIsdp #firecat注:这是mqtt协议v3.1.0版本的意思。v3.1.1该值是MQTT。
Version: 3 #firecat注:这是mqtt协议v3.1.0版本的意思。v3.1.1该值是4。
Connect Flags: 0xc2
Keep Alive: 300
Client ID Length: 15
Client ID: 861694030142473
User Name Length: 12
User Name: libaineu2004
Password Length: 8
Password: 12345678
2、服务器回应
MQ Telemetry Transport Protocol, Connect Ack
Header Flags: 0x20 (Connect Ack)
0010 .... = Message Type: Connect Ack (2)
.... 0... = DUP Flag: Not set
.... .00. = QoS Level: At most once delivery (Fire and Forget) (0)
.... ...0 = Retain: Not set
Msg Len: 2
Acknowledge Flags: 0x00
Return Code: Connection Accepted (0)
整理成文本是这样子的:
0000 10 35 00 06 4d 51 49 73 64 70 03 c2 01 2c 00 0f .5..MQIsdp...,..
0010 38 36 31 36 39 34 30 33 30 31 34 32 34 37 33 00 861694030142473.
0020 0c 6c 69 62 61 69 6e 65 75 32 30 30 34 00 08 31 .libaineu2004..1
0030 32 33 34 35 36 37 38 2345678
MQTT Connect Command
10 35 00 06 4d 51 49 73 64 70 03 c2 01 2c 00 0f 38 36 31 36 39 34 30 33 30 31 34 32 34 37 33 00 0c 6c 69 62 61 69 6e 65 75 32 30 30 34 00 08 31 32 33 34 35 36 37 38
Connect Ack
20 02 00 00
注意:如果客户端三次握手之后,不发起MQTT的CONNECT命令,30s之后会被服务器断掉。
emqttd.config文件有定义这个时间间隔:
%% Client {client, [ %% Socket is connected, but no 'CONNECT' packet received {idle_timeout, 30} %% seconds ]},
二、客户端订阅
1、客户端订阅消息,主题是"r",QoS==0
MQ Telemetry Transport Protocol, Subscribe Request
Header Flags: 0x82 (Subscribe Request)
Msg Len: 6
Message Identifier: 1
Topic Length: 1
Topic: r
Requested QoS: At most once delivery (Fire and Forget) (0)
2、服务器回应
MQ Telemetry Transport Protocol, Subscribe Ack
Header Flags: 0x90 (Subscribe Ack)
Msg Len: 3
Message Identifier: 1
Granted QoS: At most once delivery (Fire and Forget) (0)
三、客户端取消订阅
MQ Telemetry Transport Protocol, Unsubscribe Request
Header Flags: 0xa2 (Unsubscribe Request)
Msg Len: 5
Message Identifier: 2
Topic Length: 1
Topic: r
取消订阅的回应
MQ Telemetry Transport Protocol, Unsubscribe Ack
Header Flags: 0xb0 (Unsubscribe Ack)
Msg Len: 2
Message Identifier: 2
四、心跳机制
MQ Telemetry Transport Protocol, Ping Request
Header Flags: 0xc0 (Ping Request)
Msg Len: 0
MQ Telemetry Transport Protocol, Ping Response
Header Flags: 0xd0 (Ping Response)
Msg Len: 0
五、客户端publish,QoS==0,主题是"r",内容是"Hello world!"
MQ Telemetry Transport Protocol, Publish Message
Header Flags: 0x30 (Publish Message)
0011 .... = Message Type: Publish Message (3)
.... 0... = DUP Flag: Not set
.... .00. = QoS Level: At most once delivery (Fire and Forget) (0)
.... ...0 = Retain: Not set
Msg Len: 15
Topic Length: 1
Topic: r
Message: Hello world!
六、客户端publish,QoS==1,主题是"r",内容是"Hello world!"
MQ Telemetry Transport Protocol, Publish Message
Header Flags: 0x32 (Publish Message)
0011 .... = Message Type: Publish Message (3)
.... 0... = DUP Flag: Not set
.... .01. = QoS Level: At least once delivery (Acknowledged deliver) (1)
.... ...0 = Retain: Not set
Msg Len: 17
Topic Length: 1
Topic: r
Message Identifier: 3
Message: Hello world!
MQ Telemetry Transport Protocol, Publish Ack
Header Flags: 0x40 (Publish Ack)
Msg Len: 2
Message Identifier: 3
七、客户端publish,QoS==2,主题是"r",内容是"Hello world!"
MQ Telemetry Transport Protocol, Publish Message
Header Flags: 0x34 (Publish Message)
0011 .... = Message Type: Publish Message (3)
.... 0... = DUP Flag: Not set
.... .10. = QoS Level: Exactly once delivery (Assured Delivery) (2)
.... ...0 = Retain: Not set
Msg Len: 17
Topic Length: 1
Topic: r
Message Identifier: 4
Message: Hello world!
如果retain==1,则:
Header Flags: 0x35 (Publish Message)
0011 .... = Message Type: Publish Message (3)
.... 0... = DUP Flag: Not set
.... .10. = QoS Level: Exactly once delivery (Assured Delivery) (2)
.... ...1 = Retain: Set
MQ Telemetry Transport Protocol, Publish Received
Header Flags: 0x50 (Publish Received)
Msg Len: 2
Message Identifier: 4
MQ Telemetry Transport Protocol, Publish Release
Header Flags: 0x60 (Publish Release)
Msg Len: 2
Message Identifier: 4
MQ Telemetry Transport Protocol, Publish Complete
Header Flags: 0x70 (Publish Complete)
Msg Len: 2
Message Identifier: 4
八、客户端关闭
MQ Telemetry Transport Protocol, Disconnect Req
Header Flags: 0xe0 (Disconnect Req)
Msg Len: 0