From 73878ef5016ba5c3f3c94853f64c54b34989ea8b Mon Sep 17 00:00:00 2001 From: Sheedon Date: Fri, 6 Nov 2020 11:17:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0mqtt=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mqtt/build.gradle | 4 ++-- .../java/org/sheedon/mqtt/OkMqttClient.java | 22 ++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/mqtt/build.gradle b/mqtt/build.gradle index ac6e774..3a06410 100644 --- a/mqtt/build.gradle +++ b/mqtt/build.gradle @@ -8,8 +8,8 @@ android { defaultConfig { minSdkVersion 15 targetSdkVersion 28 - versionCode 7 - versionName "1.2.1" + versionCode 8 + versionName "1.3" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles 'consumer-rules.pro' diff --git a/mqtt/src/main/java/org/sheedon/mqtt/OkMqttClient.java b/mqtt/src/main/java/org/sheedon/mqtt/OkMqttClient.java index 17a1903..7e497b0 100644 --- a/mqtt/src/main/java/org/sheedon/mqtt/OkMqttClient.java +++ b/mqtt/src/main/java/org/sheedon/mqtt/OkMqttClient.java @@ -98,13 +98,33 @@ private void connect() { if (!mqttClient.isConnected() && !isStartConnect) { try { isStartConnect = true; - mqttClient.connect(connectOptions, null, listener); + mqttClient.connect(connectOptions, null, mCreateCallback); } catch (MqttException e) { e.printStackTrace(); } } } + private long lastTime = 0; + + public void reConnect() { + long nowTime = System.currentTimeMillis(); + if (nowTime - lastTime < 5000) { + isStartConnect = false; + if (listener != null) + listener.onFailure(null, new Throwable("Only reconnect once within 5 seconds")); + return; + } + + lastTime = nowTime; + try { + connect(); + } catch (Exception e) { + if (listener != null) + listener.onFailure(null, e); + } + } + /** * 获取调度器 */