PING / DISCONNECT / AUTH 报文格式
OASIS MQTT 5.0:§3.12 PINGREQ、§3.13 PINGRESP、§3.14 DISCONNECT、§3.15 AUTH
Keep Alive / 会话行为见:02-控制报文与会话.md、11-CONNECT与CONNACK报文格式.md
1. PINGREQ(§3.12)
| 项 | 值 |
|---|---|
| Type | 12 |
| Flags | 0000 |
| Remaining Length | 0 |
| Variable Header / Payload | 无 |
用途:在 Keep Alive 周期内保活;证明 Client 存活且连接可用。
1 | Fixed Header 仅 2 字节:0xC0 0x00 |
2. PINGRESP(§3.13)
| 项 | 值 |
|---|---|
| Type | 13 |
| Flags | 0000 |
| Remaining Length | 0 |
1 | Fixed Header:0xD0 0x00 |
Server 必须响应 PINGREQ。Client 若收不到 PINGRESP,应按规范处理为连接故障。
3. DISCONNECT(§3.14)
3.1 方向
- MQTT 5:Client→Server 与 Server→Client 均可发送
- MQTT 3.1.1:主要是 Client→Server;且报文极简
3.2 Fixed Header
Type=14,Flags=0000。
3.3 Variable Header(MQTT 5)
1 | Reason Code (1 byte) → Properties |
Remaining Length 可为 0(等价 Reason=0x00 且无属性,规范允许的简写形式)。
常见 Reason Code
| Code | 含义(示例) |
|---|---|
0x00 |
Normal disconnection |
0x04 |
Disconnect with Will Message(Client 可请求仍发 Will) |
0x80 |
Unspecified error |
0x81 |
Malformed Packet |
0x82 |
Protocol Error |
0x87 |
Not authorized |
0x88 |
Server busy |
0x89 |
Server shutting down |
0x8B |
Keep Alive timeout(Server→Client) |
0x8D |
Session taken over |
0x8E |
Topic Filter invalid |
0x90 |
Topic Name invalid |
0x93 |
Receive Maximum exceeded |
0x94 |
Topic Alias invalid |
0x95 |
Packet too large |
0x96 |
Message rate too high |
0x98 |
Administrative action |
0x9C |
Use another server |
0x9D |
Server moved |
0xA0 |
Maximum connect time |
0xA1 |
Subscription Identifiers not supported 等 |
常见 Properties
| 属性 | 作用 |
|---|---|
| Session Expiry Interval | Client 可在断开时改会话过期(有条件) |
| Reason String | 可读原因 |
| User Property | 自定义 |
| Server Reference | 指向另一服务器 |
3.4 正常断开 vs 异常断开
| 情况 | Will |
|---|---|
| 发送 DISCONNECT(正常原因)后关连接 | 通常不发布 Will |
| 网络中断 / Keep Alive 超时 / 未发 DISCONNECT | 可触发 Will(若已配置) |
MQTT 5 的 0x04 Disconnect with Will Message 允许显式要求发送 Will。
3.5 MQTT 3.1.1 DISCONNECT
- 仅 Fixed Header:
0xE0 0x00 - 无 Reason / Properties
4. AUTH(§3.15,仅 MQTT 5)
用于 Enhanced Authentication 多步交换(CONNECT 中声明 Authentication Method)。
4.1 Fixed Header
Type=15,Flags=0000。
4.2 Variable Header
1 | Authenticate Reason Code → Properties |
| Reason(例) | 含义 |
|---|---|
0x00 |
Success |
0x18 |
Continue authentication |
0x19 |
Re-authenticate |
Properties 必含/常含:
| 属性 | 作用 |
|---|---|
| Authentication Method | 方法名(与 CONNECT 一致) |
| Authentication Data | 方法相关二进制挑战/应答 |
| Reason String / User Property | 诊断 |
4.3 典型流程(概念)
1 | Client ── CONNECT (Auth Method=…) ──▶ Server |
亦可在已连接状态下 Re-authenticate。
5. 运维抓包提示
| 现象 | 看什么 |
|---|---|
周期性 C0 00 / D0 00 |
Keep Alive 正常 |
Server 发 DISCONNECT 0x8B |
Keep Alive 超时 |
Server 发 DISCONNECT 0x8D |
同 ClientID 被顶号 |
| AUTH 循环失败 | Method/Data 或 Broker 插件配置 |
正在加载留言…