订阅Paho MQTT经纪人时出错

时间:2019-05-21 13:05:50

标签: java mqtt paho

使用用户名和密码订阅MQTT代理(org.eclipse.paho.client.mqttv3.MqttClient)时遇到错误:

版本:

1558438891: New connection from 192.168.50.128 on port 1883.
1558438891: |-- mosquitto_auth_unpwd_check(testUser)
1558438891: |-- ** checking backend sqlite
1558438891: |-- getuser(testUser) AUTHENTICATED=1 by sqlite
1558438891: New client connected from 192.168.50.128 as TestApp (c1, k60, u'testUser').
1558438891: No will message specified.
1558438891: Sending CONNACK to TestApp (0, 0)
1558438891: Received SUBSCRIBE from TestApp
1558438891:     MyTopic/# (QoS 1)
1558438891: Sending SUBACK to TestApp
1558438891: Socket error on client TestApp, disconnecting.
1558438891: |-- mosquitto_auth_acl_check(..., client id not available, testUser, MyTopic/#, MOSQ_ACL_WRITE)
1558438891: |-- aclcheck(testUser, MyTopic/#, 4) CACHEDAUTH: 17

Error @ Broker

options = new MqttConnectOptions();
//...
//...
options.setCleanSession(true);
options.setUserName(username);
options.setPassword(password.toCharArray());
//...

client = new MqttClient(clientHostname.toString(), "TestApp"); ===> OK
//...
client.subscribe(lS.getSubscriptionTopic(), 1); ===> Error
    MqttException (128)
    at org.eclipse.paho.client.mqttv3.MqttClient.subscribe(MqttClient.java:438)
    at org.eclipse.paho.client.mqttv3.MqttClient.subscribe(MqttClient.java:424)

代码段:

AndroidManifest.xml

1 个答案:

答案 0 :(得分:0)

与paho MQTT经纪人合作时,我遇到了同样的异常。

MqttException (128)

对我来说,问题在于MQTT paho客户端一次只能订阅50个订阅主题。如果我尝试使用相同的MQTT客户端订阅第51个订阅主题,则会收到上述异常。

解决方案是在收到有关主题的回复后取消订阅主题。

org.eclipse.paho.client.mqttv3.MqttClient mqttClient;

mqttClient.subscribe(String subscribeTopic);
mqttClient.publish(String publishTopic, String payload);

//Read the message published back on the subscribe topic.
//Now unsubscribe from that subscribe topic.

mqttClient.unsubscribe(String subscribeTopic);