如何使用MqttClient的取消阻止方法 [英] How to use the un-blocking method of MqttClient

查看:564
本文介绍了如何使用MqttClient的取消阻止方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用下面的代码连接到mosquitto代理时,如您所知,连接到代理可能需要几秒钟/分钟,在此期间,当按下按钮进行连接时,它将一直保持按下状态直到连接成功为止建立并在建立连接时将按钮释放回其正常状态.据我所知,有两种使用paho java API连接客户端的方法,即blocking methodunblocking method.我的问题是,如何使用unblocking method? beow是我尝试使用阻止方法

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

代码_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.");
                }
            }
        }

推荐答案

什么按钮?建立连接几乎是即时的.

what button? establishing a connection is almost instantly.

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

There are asynchronous versions of mqtt. Code samples for that. If you want to make the synchronous non-blocking. You could start it up in another thread.

这篇关于如何使用MqttClient的取消阻止方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆