562935844@qq.com
2023-08-31 fdcf461fbfa3bcd650685743e891ad3357898f0c
HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/SocketBoot.java
@@ -1,13 +1,7 @@
package com.hdl.sdk.socket;
import android.text.TextUtils;
import android.util.Log;
import androidx.collection.ArrayMap;
import com.google.gson.JsonObject;
import com.hdl.sdk.common.config.TopicConstant;
import com.hdl.sdk.common.utils.IdUtils;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.ThreadToolUtils;
import com.hdl.sdk.socket.annotation.ConnectStatus;
@@ -15,15 +9,11 @@
import com.hdl.sdk.socket.client.IHeartbeat;
import com.hdl.sdk.socket.listener.SendListener;
import java.net.ConnectException;
import java.util.Date;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * Created by Tong on 2021/9/26.
@@ -38,18 +28,20 @@
    private final IClient client;
    private IHeartbeat iHeartbeat;
    public void SetHeartbeat(IHeartbeat iHeartbeat){
        this.iHeartbeat=iHeartbeat;
    public void SetHeartbeat(IHeartbeat iHeartbeat) {
        this.iHeartbeat = iHeartbeat;
    }
    /**
     * 当前接收到数据的时间
     */
    private static long time=System.currentTimeMillis();
    private static long time = System.currentTimeMillis();
    /**
     * tcp是否已经连接
     */
    private boolean connected=false;
    private boolean connected = false;
    public IClient getClient() {
        return client;
@@ -57,7 +49,7 @@
    private final BlockingQueue<SocketRequest> mMessageQueue = new LinkedBlockingDeque<>();
    private final ArrayMap<String, SendListener> sendMap = new ArrayMap<>();
    private final ConcurrentMap<String, SendListener> sendMap = new ConcurrentHashMap<>();
    public SocketBoot(IClient client) {
        this.client = client;
@@ -74,12 +66,13 @@
        try {
            LogUtils.i("TCP连接");
            client.onConnectStatus(ConnectStatus.CONNECTING);
            Thread.sleep(700);
            Thread.sleep(1000);
            client.connect();
            connected=true;
            connected = true;
            LogUtils.i("TCP连接成功");
            client.onConnectStatus(ConnectStatus.CONNECTED);
        }catch(Exception e) {
            LogUtils.e("连接异常"+e.getMessage());
        } catch (Exception e) {
            LogUtils.e("连接异常" + e.getMessage());
        }
    }
@@ -95,11 +88,19 @@
                public void run() {
                    while (true) {
                        try {
                            if (!connected) {
                                Thread.sleep(200);
                                continue;
                            }
                            SocketRequest socketRequest = mMessageQueue.take();
//                            final String sendStr = new String(socketRequest.getData(), 0, socketRequest.getData().length);
//                            LogUtils.i("发送数据:" + sendStr);
                            if (System.currentTimeMillis() - socketRequest.getTime() > 5000) {
                                continue;
                            }
                            final String action = socketRequest.getAction();
                            try {
                                client.sendMsg(socketRequest.getData());
                                if (!TextUtils.isEmpty(action)) {
                                    SendListener sendListener = sendMap.get(action);
@@ -140,7 +141,7 @@
                            if (connected) {
                                //读取数据
                                client.onHandleResponse();
                                time= System.currentTimeMillis();
                                time = System.currentTimeMillis();
                            } else {
                                try {
                                    Thread.sleep(1000);
@@ -173,9 +174,9 @@
                            if (!connected) {
                                reconect();
                            }
                            Thread.sleep(3*1000);
                            Thread.sleep(3 * 1000);
                        } catch (Exception e) {
                            LogUtils.e("定时连接失败:" + e.getMessage());
                        }
                    }
                }
@@ -198,15 +199,12 @@
                            if (connected && 10 * 1000 < (System.currentTimeMillis() - time)) {
                                time = System.currentTimeMillis();
                                //心跳检测
                                try {
                                    if (iHeartbeat != null)
                                        iHeartbeat.heartbeat();
                                } catch (Exception e) {
                                }
                                if (iHeartbeat != null)
                                    iHeartbeat.heartbeat();
                            }
                            Thread.sleep(1000);
                        } catch (Exception e) {
                            LogUtils.e("定时心跳检测网关异常:" + e.getMessage());
                        }
                    }
                }
@@ -225,6 +223,7 @@
    /**
     * 发送无需回调
     *
     * @param msg 发送的数据
     */
    public void sendMsg(byte[] msg) {
@@ -243,7 +242,7 @@
            }
            mMessageQueue.put(request);
        } catch (Exception e) {
            LogUtils.e("发送数据失败:" + e.getMessage());
        }
    }