问题描述
我正在使用HiveMQ客户端,我想知道是否有一种方法可以获取客户端订阅的服务质量(QoS)(就特定主题而言还是一般而言)?我会寻找一种可以像这样在客户端上调用的方法:
I'm working with HiveMQ Client and I wanted to know if there was a way to get the quality of service (QoS) that a client is subscribing with (in terms of a specific topic or in general)? I would be looking for a method I could invoke on a client like so:
Mqtt5BlockingClient subscriber = Mqtt5Client.builder()
.identifier(UUID.randomUUID().toString()) // the unique identifier of the MQTT client
.serverHost("localhost")
.serverPort(1883)
.buildBlocking();
subscriber.getQoS("topic") // returns the QoS of the subscriber is subscribing to the given topic
我只需要这些信息,以便可以打印到控制台中.
I would just want this information so I could print in to the console.
推荐答案
我认为您必须阅读有关MQTT概念的更多信息.服务质量(QoS)级别是消息的发送者和接收者之间关于保证传递消息的协议.因此,在publish()
和subscribe()
方法中使用QoS,而不是connect()
.
I think you have to read more about MQTT concepts.The Quality of Service (QoS) level is an agreement between a sender and receiver of a message regarding the guarantees of delivering a message.Therefore, the QoS is used in the publish()
and subscribe()
methods not the connect()
.
这是场景:
1.连接:
您必须将客户端连接到使用用户名/密码的任何代理.每个mqtt库都有一个connect()
方法.在此步骤中,您尚未指定qos.
成功连接后(每个mqtt库都有一个connect方法的回调),您可以发布或订阅任何想要的(或允许的)主题.
示例:
Eclipse Paho库:
1. Connect:
You have to connect your client to any broker with username/password. Every mqtt library has a connect()
method. In this step, you have not specified qos yet.
After successful connection (every mqtt library has a callback for the connect method) and you can publish or subscribe to any desired (or allowed) topics.
Example:
Eclipse Paho library:
IMqttToken token = clientPhone.connect();
client.connect();
//or
client.connectWith().keepAlive(10).send();
//or
Mqtt5Connect connectMessage = Mqtt5Connect.builder().keepAlive(10).build();
client.connect(connectMessage);
2.发布:
当您要publish()
一条消息时,必须指定一个QoS,以便代理将根据此QoS响应客户端:
2. Publish:
When you want to publish()
a message, you have to specify a qos, so that the broker will respond to the client according with this qos:
Qos=0:
Client ---- Publish method ----> broker
Qos=1:
Client ---- Publish method ----> broker
Client <---- PubAck callback ---- broker
Qos=2:
Client ---- Publish method ----> broker
Client <---- PubRec callback ---- broker
Client ---- PubRel method ----> broker
Client <---- PubComp callback ---- broker
示例:
IMqttDeliveryToken tokenPub = clientPhone.publish(topicPub, message);
client.publishWith()
.topic("test/topic")
.qos(MqttQos.AT_LEAST_ONCE)
.payload("payload".getBytes())
.send();
//or:
Mqtt5Publish publishMessage = Mqtt5Publish.builder()
.topic("test/topic")
.qos(MqttQos.AT_LEAST_ONCE)
.payload("payload".getBytes())
.build();
client.publish(publishMessage);
3.订阅:
SUBSCRIBE消息可以包含任意数量的客户端订阅.每个订阅都是一对主题和QoS级别.订阅消息中的主题还可以包含通配符,这使得可以订阅某些主题模式.如果一个客户端的订阅重叠,则该主题的最高QoS级别将获胜,并且将由代理用于传递消息.
示例:
3. Subscribe:
A SUBSCRIBE message can contain an arbitrary number of subscriptions for a client. Each subscription is a pair of a topic and QoS level. The topic in the subscribe message can also contain wildcards, which makes it possible to subscribe to certain topic patterns. If there are overlapping subscriptions for one client, the highest QoS level for that topic wins and will be used by the broker for delivering the message.
Example:
IMqttToken subToken = MqttAndroidClientInstance.subscribe(topics, qos);
client.subscribeWith().topicFilter("test/topic").qos(MqttQos.EXACTLY_ONCE).send();
//or:
Mqtt5Subscribe subscribeMessage = Mqtt5Subscribe.builder()
.topicFilter("test/topic")
.qos(MqttQos.EXACTLY_ONCE)
.build();
client.subscribe(subscribeMessage);
编辑(1):
如果要在重新连接后接收已订阅的主题,则mqtt客户端必须使用以下参数:
A-与 cleanSession false 连接.
B-订阅 QOS> 0 .
Edit(1):
A mqtt client have to use the following parameters, if wants to receive the already subscribed topics after reconnecting:
A- connect with cleanSession false.
B- Subscribe with QOS>0.
这篇关于如何在HiveMQ客户端中获取客户端的QoS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!