我無法找到重新連接mqtt回撥客戶端的邏輯。有方法onDisconnected(),但我無法找到文檔或任何互聯網上的示例。MQTT回撥客戶端重新連接邏輯
我的聽衆
公共類myListener的實現監聽 {
public MyListener()
{
}
@Override
public void onConnected()
{
System.out.println("Connected ....");
}
@Override
public void onDisconnected()
{
System.out.println("Disconnected");
}
@Override
public void onPublish(UTF8Buffer topic, Buffer body, Runnable ack)
{
System.out.println("Entered Onpublish");
try
{
System.out.println("received msg:" + msg);
}
catch (HikeException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
ack.run();
}
}
@Override
public void onFailure(Throwable value)
{
value.printStackTrace();
}
}
創建連接
private void createConnection(String host, int port,String id, String token) throws Exception
{
this.disconnect();
MQTT mqtt = new MQTT();
mqtt.setHost(host, port);
mqtt.setUserName(id);
mqtt.setPassword(token);
CallbackConnection callbackConnection = null;
callbackConnection = mqtt.callbackConnection();
callbackConnection.listener(new MyListener());
callbackConnection.connect(new MyCallback<Void>("CONNECT"));
callbackConnection.subscribe(new Topic[] { new Topic(uid + "/u", QoS.AT_MOST_ONCE) }, new MyCallback<byte[]>("EVENT SUBSCRIBE"));
callbackConnection.subscribe(new Topic[] { new Topic(uid + "/s", QoS.AT_LEAST_ONCE), new Topic(uid + "/a", QoS.AT_LEAST_ONCE) }, new MyCallback<byte[]>("MSG SUBSCRIBE"));
this.callbackConnection = callbackConnection;
}
myCallBack函數
class MyCallback<T> implements Callback<T>
{
public MyCallback(String tag)
{
super();
this.tag = tag;
}
String tag;
@Override
public void onSuccess(T value)
{
System.out.println("TAG:" + tag + " =SUCCESS value=" + value);
}
@Override
public void onFailure(Throwable value)
{
System.out.println("TAG:" + tag + "Fail");
value.printStackTrace();
}
}
我的問題是如何實現mqtt重新連接到服務器邏輯?如果我應該使用onDisconnect()方法,那我該如何使用它?
? – knolleary