MQTT v5.0------SUBSCRIBE 报文

SUBSCRIBE message

Fixed header:

 

 

The remaining length field 
indicates the length of the variable length of the header plus the payload is a variable-length encoded byte integers.

Variable header

SUBSCRIBE variable header packet sequence contains the following fields: packet identifier (Packet Identifier), attributes (Properties).

 

Example: a variable header (no attribute), message identifier 10

 

 

 

 

Property exists SUBSCRIBE variable packet header.

  • 11 (0x0B), subscription identifier (Subscription Identifier) ​​identifiers. In the range from 1 to 268,435,455. . Subscription identifier comprises a value of 0 or more subscription identifier will result in a protocol error.
  • 38 (0x26), user attributes (User Property) identifier.

 

.SUBSCRIBE load

 

 

MQTT 5.0 SUBSCRIBE message in the Payload contains the subscription options (Subscription Options).

SUBSCRIBE packet payload contains a topic filter, specify the subject of the client wish to subscribe to. Topic filter must be UTF-8 encoded string. Each topic is followed by a subscription option (Subscription Options) byte after the filter.

Subscription Options (Subscription Options) of 0 and 1 represents maximum QoS. This field gives the maximum QoS level server may send messages to the client application. If QoS is 3, it will trigger a protocol error.

The first two subscription options represent a non-local option (No Local). If the value is 1, the application will not publish messages to subscribers Post subject issuer itself, if the share subscription, then this option is set to 1, it will trigger a protocol error.

The first three subscription options expressed reservations as published (Retain As Published). If the value is 1, the server forwards the message RETAIN flag shall be set consistent with the received PUBLISH message RETAIN flag. If the value is 0, RETAIN flag regardless received PUBLISH message is any value, the server needs to forward the message to RETAIN flag is set to 0.

4 of 5 subscription options expressed reservations processing (Retain Handling). This option indicates when the subscription is established, whether to send a reservation message. Send any reservation message after this option has no effect. If there is no match to retain the message topic filter, the behavior of this option all values ​​are the same. Value can be set to:

  • 0 = Send message Subscribe to establish reserved
  • 1 = establish subscription, if the subscription does not exist currently sending a reservation message
  • 2 = Do not send reservation subscribe to establish message

No. 6 7 subscription options are reserved for future use. Any non-zero if a bit reserved payload, the server message will be treated as malformed packets.

 

Do not send a message to retain the case of an existing subscription is useful, for example, when reconnection complete the client uncertain whether the subscription is created in the previous session connection.

Do not send messages to subscribers saved to retain the newly created is very useful, such as the client wishes to receive change notifications and does not need to know the initial state.

For some indication that it does not support server-side message retention, retention and release processing options keep all valid values you will get the same result: no reservations send any message subscription, and reserved sign all messages will be set to zero .

 

example:

 

 SUBSCRIBE behavior

When the server receives SUBSCRIBE message from the client, you must use the message as appropriate SUBACK

 

SUBACK packet must be acknowledged SUBSCRIBE message has the same message identifier

Allow the server before sending SUBACK began sending messages that match the subscription PUBLISH message

如果服务端收到的SUBSCRIBE报文中的一个主题过滤器与当前会话的一个非共享订阅(Non-shared  Subscription)相同,那么必须使用新的订阅替换现存的订阅 。新订阅的主题过滤器与之前 的订阅相同,但其订阅选项可能不同。如果保留处理选项为0,任何匹配该主题过滤器的保留消息必须被重发,但替换订阅不能造成应用消息的丢失 。

如果服务端收到的非共享主题过滤器(Non-shared Topic Filter)不同于当前会话的任何主题过滤器,一个新的非共享订阅将被创建。如果保留处理选项不为2,所有相匹配的保留消息将发送给客户端。

如果服务端收到的主题过滤器与服务端已存在的某个共享订阅(Shared Subscription)主题过滤器相同,则将此会话添加到该共享订阅中。不发送任何保留消息。

如果服务端收到的共享订阅主题过滤器(Shared Subscription Topic Filter)与任何已存在的共享订阅主题 过滤器都不同,一个新的共享订阅将被创建。将此会话作为订阅者添加到该共享订阅。不发送任何保留消息。

如果服务端收到的SUBSCRIBE报文包含多个主题过滤器,服务端必须当做收到一系列多个SUBSCRIBE 报文来处理--除了将它们的响应组合为单个SUBACK响应

服务端发送给客户端的SUBACK报文必须为每一个主题过滤器/订阅选项对包含一个原因码,此原因码必须说明为该订阅授予的最大QoS等级,或指示订阅失败

。服务端可能授予了低于订阅者所请求的最大QoS等级。响应该订阅的应用消息QoS等级必须为该消息发布时的QoS等级和服务端授予的最大QoS等级二者最小值

在原始消息发布的QoS等级为1,且授予的 最大QoS等级为0的情况下,服务端允许发送重复的消息副本给订阅者。

如果订阅客户端的某个主题过滤器已被授予的最大QoS等级为1,那么匹配此过滤器的QoS等级为0的应用消息按照QoS等级为0分发给此客户端。这意味着客户端最多只能收到该消息的一个 副本。

另一方面,发布到相同主题的QoS等级为2的消息,其QoS等级被服务端降级为1以便分 发给该客户端。因此该客户端可能收到此消息的多个副本。

如果订阅客户端被授予的最大QoS等级为0,那么按照QoS等级为2发布的应用消息在繁忙时可 能会丢失,但服务端不应该发送重复的消息副本。发布到相同主题的QoS等级为1的消息,分发 给该客户端时可能会丢失或重复。

 

Guess you like

Origin www.cnblogs.com/saryli/p/11520435.html