0

When I try the below code to connect to the mosquitto broker, as you know, connecting to the broker might takes few seconds/minutes, and during that time when the button pressed to connect, it remains pressed till the connection established and when the connection established the button released back to its normal state. As far as I know, there are two way for connecting a client using paho java API, the blocking method and unblocking method. my question is, how to use the unblocking method? beow is my attempt to use the blocking method

Code_1:

//mqttFactory
public final class MQTTClientFactory {

public static MqttClient newClient(String ip, int port, String clientID) throws MqttException {

    String serverURI = formURI(ip, port);
    MqttClient client = new MqttClient(serverURI, clientID).;
    return client;
}



MqttConnectOptions opts = getClientOptions();
        client = MQTTClientFactory.newClient(broker, port, clientID);

        if (client != null) {
            System.out.println("Client is not Null");
            client.setCallback(AsynchCallBack);
            if (opts != null) {
                client.connectWithResult(opts).setActionCallback(synchCallBack);
                if (client.isConnected()) {
                    System.out.println("Client CONNECTED.");
                }
            }
        }
4

1 回答 1

0

什么按钮?几乎立即建立连接。

mqtt 有异步版本。代码示例。如果要使同步非阻塞。您可以在另一个线程中启动它。

于 2015-11-20T22:00:22.580 回答