mqtt 连接
This commit is contained in:
@@ -161,7 +161,7 @@ public class CommonAppContext extends MultiDexApplication implements Applicatio
|
||||
//设置mqtt环境 false 测试环境 true 正式环境
|
||||
// ExternalResConstants.INSTANCE.setIS_MQTT_RELEASE(false);
|
||||
//设置http环境 false 测试环境 true 正式环境
|
||||
ExternalResConstants.INSTANCE.setIS_HTTP_RELEASE(true);
|
||||
ExternalResConstants.INSTANCE.setIS_HTTP_RELEASE(false);
|
||||
currentEnvironment = ExternalResConstants.INSTANCE.HTTP_PATH();
|
||||
|
||||
|
||||
@@ -450,9 +450,9 @@ public class CommonAppContext extends MultiDexApplication implements Applicatio
|
||||
// startService(mqttServiceIntent);
|
||||
// }
|
||||
|
||||
mqttConnect=MqttConnect.getInstance(this, currentEnvironment.getMqttUrl(),"android-"+ MqttClient.generateClientId());
|
||||
// mqttConnect=MqttConnect.getInstance(this, currentEnvironment.getMqttUrl(),"android-"+ MqttClient.generateClientId());
|
||||
// mqttConnect=MqttConnect.getInstance(this,"tcp://1.13.101.98","android-"+ MqttClient.generateClientId());
|
||||
mqttConnect.mqttClient();
|
||||
// mqttConnect.mqttClient();
|
||||
|
||||
// 每次启动应用时重置状态
|
||||
SpUtil.getInstance().setBooleanValue("youth_model_shown", false);
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.os.Looper;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.blankj.utilcode.util.ActivityUtils;
|
||||
import com.blankj.utilcode.util.LogUtils;
|
||||
import com.google.android.gms.common.api.Api;
|
||||
import com.hjq.toast.ToastUtils;
|
||||
@@ -61,6 +62,7 @@ public class MqttConnect {
|
||||
|
||||
/**
|
||||
* 单列模式,只能实例化一次
|
||||
*
|
||||
* @param context
|
||||
* @param host
|
||||
* @param clientId
|
||||
@@ -76,51 +78,44 @@ public class MqttConnect {
|
||||
/**
|
||||
* 客户端connect连接mqtt服务器
|
||||
**/
|
||||
public void mqttClient()
|
||||
{
|
||||
// close();
|
||||
// handler.postDelayed(new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
public void mqttClient() {
|
||||
close();
|
||||
|
||||
// 使用线程或线程池
|
||||
new Thread(() -> {
|
||||
try {
|
||||
// uiTip("MQTT开始连接");
|
||||
MqttConnectOptions options = mqttConnectOptions();
|
||||
mqttClient.setCallback(new MqttInitCallback(context, HOST, clientId));
|
||||
// 在子线程连接(不会阻塞UI)
|
||||
mqttClient.connect(options);
|
||||
// 订阅也在子线程
|
||||
// sub(topic, qos);
|
||||
sub(shutdown);
|
||||
sub(update_app);
|
||||
sub(qx_hour_ranking);
|
||||
sub(qx_redpacket_arrive);
|
||||
// uiTip("MQTT连接成功");
|
||||
}catch (MqttException e){
|
||||
// uiTip("MQTT连接失败,准备重连。。。:"+e.getMessage());
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
LogUtils.e(Tag,"开始重连。。。");
|
||||
mqttClient();
|
||||
|
||||
// UI操作回到主线程
|
||||
ActivityUtils.getTopActivity().runOnUiThread(() -> uiTip("MQTT连接成功"));
|
||||
|
||||
} catch (Exception e) {
|
||||
ActivityUtils.getTopActivity().runOnUiThread(() -> {
|
||||
uiTip("MQTT连接失败,准备重连: " + e.getMessage());
|
||||
});
|
||||
// 延迟重连
|
||||
handler.postDelayed(() -> mqttClient(), 3000);
|
||||
}
|
||||
},3000);
|
||||
}
|
||||
// }
|
||||
// },200);
|
||||
}).start();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 在主线程弹出消息
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
private void uiTip(String msg) {
|
||||
LogUtils.d(Tag, msg);
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Toast.makeText(context.getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
|
||||
LogUtils.e("mqtt","连接成功");
|
||||
ToastUtils.show(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,7 +126,6 @@ public class MqttConnect {
|
||||
mqttClient = new MqttClient(HOST, clientId, new MemoryPersistence());
|
||||
MqttConnectOptions options = new MqttConnectOptions();
|
||||
options.setUserName("public");
|
||||
options.setConnectionTimeout(10);
|
||||
options.setCleanSession(true);
|
||||
options.setConnectionTimeout(10);
|
||||
options.setKeepAliveInterval(10);
|
||||
@@ -203,8 +197,7 @@ public class MqttConnect {
|
||||
* 订阅某一个主题,可携带Qos
|
||||
*
|
||||
* @param topic 所要订阅的主题
|
||||
* @param qos
|
||||
* 消息质量:0最多发送一次,不保证消息能够到达接收端,也不负责重发
|
||||
* @param qos 消息质量:0最多发送一次,不保证消息能够到达接收端,也不负责重发
|
||||
* 1至少发送一次,确保消息能够到达接收端,但可能会导致消息重复
|
||||
* 2确保消息恰好被接收一次
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user