Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MqttAsyncClient客户端未连接(32104)_Java_Mqtt - Fatal编程技术网

Java MqttAsyncClient客户端未连接(32104)

Java MqttAsyncClient客户端未连接(32104),java,mqtt,Java,Mqtt,我正在Java中使用MqttAsyncClient。下面的代码只是我如何使用它的一部分,当我试图订阅任何主题时,如果我遇到错误,问题就会出现: 客户端未连接(32104) 这是我得到的错误: [INFO ] 2019-10-12 21:56:37.234 [main] SpManager - Starting the Mqtt Configuration... [INFO ] 2019-10-12 21:56:37.388 [main] SpManager - The Mqtt protocol

我正在Java中使用MqttAsyncClient。下面的代码只是我如何使用它的一部分,当我试图订阅任何主题时,如果我遇到错误,问题就会出现:

客户端未连接(32104)

这是我得到的错误:

[INFO ] 2019-10-12 21:56:37.234 [main] SpManager - Starting the Mqtt Configuration...
[INFO ] 2019-10-12 21:56:37.388 [main] SpManager - The Mqtt protocol has been configured successfully!!!
[INFO ] 2019-10-12 21:56:37.388 [main] SpManager - Conecting to: tcp://0.tcp.ngrok.io:xxxxx Mqtt Server
[INFO ] 2019-10-12 21:56:37.406 [main] SpManager - Connected to the: tcp://0.tcp.ngrok.io:xxxxx Mqtt Server
[INFO ] 2019-10-12 21:56:37.406 [main] SpManager - Subscribe to the Topic: home/Monitoreo/#
[ERROR] 2019-10-12 21:56:37.427 [main] SpManager - An error has happened: Client is not connected (32104)
[ERROR] 2019-10-12 21:56:37.428 [main] SpManager - 
msg Client is not connected
loc Client is not connected

最后,我想指出,当我使用MqttClient而不是MqttAsyncClient

时,此代码可以完美地工作。问题是,您没有在订阅之前等待连接完成。这是因为MqttClient会阻止其中的MqttAsyncClient返回一个令牌,该令牌可用于检查操作何时完成

快速修复方法是在连接()之后插入一个
waitForCompletion()


它真的工作得很完美!!!现在我的问题是,一旦订阅完成,我就无法收到任何已发布的消息。谢谢
[INFO ] 2019-10-12 21:56:37.234 [main] SpManager - Starting the Mqtt Configuration...
[INFO ] 2019-10-12 21:56:37.388 [main] SpManager - The Mqtt protocol has been configured successfully!!!
[INFO ] 2019-10-12 21:56:37.388 [main] SpManager - Conecting to: tcp://0.tcp.ngrok.io:xxxxx Mqtt Server
[INFO ] 2019-10-12 21:56:37.406 [main] SpManager - Connected to the: tcp://0.tcp.ngrok.io:xxxxx Mqtt Server
[INFO ] 2019-10-12 21:56:37.406 [main] SpManager - Subscribe to the Topic: home/Monitoreo/#
[ERROR] 2019-10-12 21:56:37.427 [main] SpManager - An error has happened: Client is not connected (32104)
[ERROR] 2019-10-12 21:56:37.428 [main] SpManager - 
msg Client is not connected
loc Client is not connected
try
{
    logger.info("Connecting to: " + conf.getServerURI() + " Mqtt Server");
    IMqttToken token = client.connect(connOpts);
    token.waitForCompletion();
    logger.info("Connected to the: "+ conf.getServerURI() + " Mqtt Server");
    client.setCallback(getCallback());
    logger.info("Subscribe to the Topic: " + this.topic);
    //Thread.sleep(1000);
    client.subscribe(this.topic, 1);
    logger.info("Successful subscription to topic: " + this.topic);
}
catch(MqttException me)
{

}