In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Brief introduction
A testamentary message sent to the relevant subscriber when the client disconnects. Will Message is sent in the following cases:
The server has an Iamp O error or network failure; the client loses contact during the defined heartbeat period; the client closes the network connection before sending the offline packet; and the server closes the network connection before receiving the offline packet.
The testamentary message is usually specified at the time of the client CONNECT. As shown below, the connection is set by calling the setWill method of the MqttConnectOptions instance. Any client that subscribes to the topic below can receive the testamentary message.
/ / method 1MqttConnectOptions.setWill (MqttTopic topic, byte [] payload, int qos, boolean retained) / / method 2MqttConnectOptions.setWill (java.lang.String topic, byte [] payload, int qos, boolean retained) usage scenario
When client A connects, the will message is set to "offline" and client B subscribes to this testamentary topic. When A disconnects abnormally, client B receives a testament message from this "offline" and knows that client An is offline.
Connect Flag message field Bit7654210User Name FlagPassword FlagWill RetainWill QoSWill FlagClean StartReservedbyte 8XXXXXXX
Testamentary messages are not sent after the client normally calls the disconnect method.
Will Flag action
In short, the client pre-defined, in the case of its own abnormal disconnection, left the last will (Last Will), also known as the will (Testament). This will is a pre-defined topic and corresponding message by the client, which is appended to the variable message header of the CONNECT. If the client connection is abnormal, the server actively publishes the message.
When the Will Flag bit is 1, Will QoS and Will Retain will be read, and the specific contents of Will Topic and Will Message must appear in the message body, otherwise the Will QoS and Will Retain values will be ignored.
When the Will Flag bit is 0, Will Qos and Will Retain are invalid.
Command line example
Here is an example of Will Message:
Predefined will message for clientid=sub on Sub:
Mosquitto_sub-- will-topic test-- will-payload die-- will-qos 2-t topic-I sub-h 192.168.1.1
Client clientid=alive subscribes to testamentary topics at 192.168.1.1 (EMQ server)
Mosquitto_sub-t test-I alive-Q 2-h 192.168.1.1
The sub side is unexpectedly disconnected from the Server side (EMQ server), and the publisher side receives the Will Message.
Advanced usage scenario
Here's how to combine Retained messages with Will messages.
The testamentary message of client An is set to "offline", and the testamentary subject is set to the same Retained as a topic in a normal sending state; when client A connects, it sends a Retained message of "online" to the topic A/status, and when other clients subscribe to the topic A/status, the Retained message is "online". When client A disconnects abnormally, the system automatically sends a "offline" message to the topic A/status, and other clients that subscribe to this topic will immediately receive a "offline" message; if the testamentary message is set to Retained, when a new client subscribing to the A/status topic comes online, the message is "offline".
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.