MQTT Protocol - Subscription Topic and Subscription Confirmation

MQTT Protocol - Subscription Topic and Subscription Confirmation

SUBSCRIBE - Subscribe to a topic

Subscription is the client subscribes to the server

insert image description here

subscribe newsletter

Subscription messages are similar to CONNECT messages, they are composed of fixed header + variable header + payload

The fixed header is relatively simple and also consists of two bytes, the first byte is 82, and the second byte is the remaining length

insert image description here

The variable header consists of two bytes, which is also basically fixed, and the hexadecimal value is 00 0A

insert image description here

The payload consists of two parts, namely the topic filter and the quality of service requirements. The topic filter can be simply understood as a topic, which corresponds to a topic with subscription permissions on the Alibaba Cloud platform. According to the combination of CONNECT messages, this topic is also To convert to hexadecimal, the quality of service requirement can be set to 00

insert image description here

After converting the subject to hexadecimal, calculate the number of bytes, put it at the beginning, set the quality of service requirement to 00, and put it at the end

insert image description here

Combining the hexadecimal numbers of the fixed header, variable header and payload, the subscription message is obtained, and the remaining length is calculated to be 0x36

insert image description here

Combination Subscription Message

Before subscribing, you must first connect to Alibaba Cloud through the CONNECT message, and then send a subscription message. Note that the client subscribes to the server

Fixed header: 82??

Variable header: 00 0A

Next, convert the bytes of the payload, find the custom Topic in the products of the Alibaba Cloud platform, select the Topic class with subscription permissions, and copy it to the text document

insert image description here

Change ${deviceName} to a specific device name

insert image description here

Use the network debugging assistant to convert to hexadecimal, first paste the Topic class in ASCII mode

insert image description here

Click the HEX of the send setting, convert the Topic to hexadecimal, copy it to a text file, click send, and check the data length

insert image description here

The length is 30, 30 is converted to hexadecimal to 1E, the length is added at the beginning of the data, and the quality of service level Qos is added at the end, set to 00

insert image description here

Adding the fixed header and the variable header, we can get

insert image description here

Calculate the number of bytes after ??, which is 35, which is less than 128. Therefore, a single byte is used to represent the remaining length, and the hexadecimal value is 0x23, so the final subscription message is as follows

insert image description here

Use the network debugging assistant to subscribe to the Alibaba Cloud device topic

First use the CONNECT message to connect to Alibaba Cloud, the Alibaba Cloud server returns 4 bytes, and the last byte is 00, which means the connection is successful

insert image description here

The device is already online

insert image description here

Send the subscription message again, and you can see that Alibaba Cloud has replied with 5 bytes, which means the subscription is successful

insert image description here

SUBACK - subscription confirmation

The server sends a SUBACK message to the client to confirm that it has received and is processing the SUBSCRIBE message.

The SUBACK packet contains a list of return codes that specify the maximum QoS class to be granted for each subscription requested by the SUBSCRIBE.

In the previous step, Alibaba Cloud replied 90 03 00 0A 01 with a total of 5 bytes, which is the subscription confirmation message

Subscription confirmation message parsing

The subscription confirmation message is also composed of three parts: fixed header + variable header + payload

The fixed header is represented by two bytes, which are basically fixed. The first byte is 90, and the second byte is the remaining length. Replace it with ??

insert image description here

The variable header consists of two bytes, which is consistent with the variable header of the subscription topic message above, which is 00 0A, that is, what this part of the client sends to the server, and the server will reply the same data. The variable header is 00 0B, so the variable header of the subscription confirmation here is also 00 0B

insert image description here

The payload contains a list of return codes, which are actually the quality of service level Qos, but there is a problem here. The level set in the subscription message is 0. From the above reply from Alibaba Cloud, it can be seen that it is 01, and the level is 1. If If the level of the subscription message is set to 1, the server will also return 01. This is a small problem, and it is not clear

insert image description here

Combining the fixed header, variable header and payload becomes a subscription confirmation message, because there are only 3 bytes after the remaining length, so the byte indicating the length is 03

The final subscription message is: 90 03 00 0A 01

Guess you like

Origin blog.csdn.net/weixin_46251230/article/details/129412941