MQTT客户端断开重连2

mqttAsyncClient.connect(connOpts).waitForCompletion();
mqttClient.setCallback(new MqttCallback() {
          @Override
           public void connectionLost(Throwable cause) {
               //连接丢失后,一般在这里面进行重连
                    if (isNetworkAvailable()) {
                        reconnectIfNecessary();
                    }
            }
           ...
});

public synchronized void reconnectIfNecessary() {
        if (mqttClient == null || !mqttClient.isConnected()) {
            connect();
        }
}

猜你喜欢

转载自my.oschina.net/matt0614/blog/1810681