应用订阅了 MQTT 主题,但缺少发布到该主题的一些消息
App is subscribed to MQTT topic but is missing some messages published to that topic
我在 Android 上使用 AWS Iot。我想获得特定事物的影子状态。这是代码:
mqttManager = new AWSIotMqttManager(clientId, CUSTOMER_SPECIFIC_ENDPOINT);
String subTopic = "$aws/things/<thingName>/shadow/get/accepted";
mqttManager.subscribeToTopic(subTopic, AWSIotMqttQos.QOS0, new AWSIotMqttNewMessageCallback() {
@Override
public void onMessageArrived(String topic, byte[] data) {
// do something
});
String pubTopic = "$aws/things/<thingName>/shadow/get";
mqttManager.publishString("", pubTopic, AWSIotMqttQos.QOS0);
奇怪的是它运行一次,然后我重新 运行 代码但它不起作用,我重新 运行 它再次运行。这是为什么?
问题是客户端在订阅完成前发布。幸运的是,当订阅完成时,subscribeToTopic 方法接受另一个 returns 回调。那是我们可以安全发布的时候。代码如下所示:
String subTopic = "$aws/things/<thingName>/shadow/get/accepted";
mqttManager.subscribeToTopic(subTopic,
AWSIotMqttQos.QOS0,
new AWSIotMqttSubscriptionStatusCallback() {
@Override
public void onSuccess() {
String pubTopic = "$aws/things/<thingName>/shadow/get";
mqttManager.publishString("", pubTopic, AWSIotMqttQos.QOS0);
}
@Override
public void onFailure(Throwable exception) {
}
},
new AWSIotMqttNewMessageCallback() {
@Override
public void onMessageArrived(String topic, byte[] data) {
// do something
}
});
我在 Android 上使用 AWS Iot。我想获得特定事物的影子状态。这是代码:
mqttManager = new AWSIotMqttManager(clientId, CUSTOMER_SPECIFIC_ENDPOINT);
String subTopic = "$aws/things/<thingName>/shadow/get/accepted";
mqttManager.subscribeToTopic(subTopic, AWSIotMqttQos.QOS0, new AWSIotMqttNewMessageCallback() {
@Override
public void onMessageArrived(String topic, byte[] data) {
// do something
});
String pubTopic = "$aws/things/<thingName>/shadow/get";
mqttManager.publishString("", pubTopic, AWSIotMqttQos.QOS0);
奇怪的是它运行一次,然后我重新 运行 代码但它不起作用,我重新 运行 它再次运行。这是为什么?
问题是客户端在订阅完成前发布。幸运的是,当订阅完成时,subscribeToTopic 方法接受另一个 returns 回调。那是我们可以安全发布的时候。代码如下所示:
String subTopic = "$aws/things/<thingName>/shadow/get/accepted";
mqttManager.subscribeToTopic(subTopic,
AWSIotMqttQos.QOS0,
new AWSIotMqttSubscriptionStatusCallback() {
@Override
public void onSuccess() {
String pubTopic = "$aws/things/<thingName>/shadow/get";
mqttManager.publishString("", pubTopic, AWSIotMqttQos.QOS0);
}
@Override
public void onFailure(Throwable exception) {
}
},
new AWSIotMqttNewMessageCallback() {
@Override
public void onMessageArrived(String topic, byte[] data) {
// do something
}
});