Skip to content

Commit

Permalink
添加mqtt重新连接方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheedon committed Nov 6, 2020
1 parent 690f837 commit 73878ef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions mqtt/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
22 changes: 21 additions & 1 deletion mqtt/src/main/java/org/sheedon/mqtt/OkMqttClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
* 获取调度器
*/
Expand Down

0 comments on commit 73878ef

Please sign in to comment.