hxb
2021-12-13 ca5001d8800e80ffe226a05e5228802bef159337
HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/SocketBoot.java
@@ -5,6 +5,7 @@
import androidx.collection.ArrayMap;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.ThreadToolUtils;
import com.hdl.sdk.socket.annotation.ConnectStatus;
import com.hdl.sdk.socket.client.IClient;
@@ -32,6 +33,10 @@
    private ScheduledExecutorService delayThread;
    private final IClient client;
    public IClient getClient() {
        return client;
    }
    /**
     * socket是否在运行
@@ -67,7 +72,7 @@
        final int maxRetry = client.getOptions().getMaxRetry();
        if (maxRetry == 0 && resendCount.get() > 0 ||
                (maxRetry > 0 && maxRetry + 1 < resendCount.get())) {
            Log.d("====", "===重连次数达到最大==");
            LogUtils.d("====", "===重连次数达到最大==");
            return;
        }
        if (!client.isConnect()) {
@@ -82,7 +87,7 @@
                        try {
                            resendCount.set(resendCount.get() + 1);
                            Thread.sleep(300L);
                            Log.d("====", "==重连第" + resendCount + "次==");
                            LogUtils.d("====", "==重连第" + resendCount + "次==");
                        } catch (Exception ignored) {
                        }
                    }
@@ -90,7 +95,7 @@
                        client.connect();
                        isRun.set(true);
                        if (client.isConnect()) {
                            Log.d("====", "====连接成功====");
                            LogUtils.d("====", "====连接成功====");
                            startHeartbeat();
                            initSendThread();
@@ -103,7 +108,7 @@
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.d("====", "===连接失败===" + e);
                        LogUtils.d("====", "===连接失败===" + e);
                        //再判断一下有没有连接
                        if (!client.isConnect()) {
                            isRun.set(false);
@@ -126,9 +131,10 @@
            public void run() {
                while (isRun.get()) {
                    if (client.isConnect()) {
                        Log.d("=====", "==发送数据==");
                        try {
                            SocketRequest socketRequest = mMessageQueue.take();
                            final String sendStr = new String(socketRequest.getData(), 0, socketRequest.getData().length);
                            LogUtils.i("发送数据:"+sendStr);
                            final String action = socketRequest.getAction();
                            try {
                                client.sendMsg(socketRequest.getData());
@@ -162,34 +168,36 @@
                    }
                }
                Log.d("=====", "==发送线程关闭==");
                LogUtils.d("=====", "==发送线程关闭==");
            }
        });
    }
    /**
     * 初始化接收线程,只需要初始化一次
     */
    public void initReceiveThread() {
        if (receiveThread == null) {
            receiveThread = ThreadToolUtils.getInstance().newFixedThreadPool(1);
        }
        receiveThread.execute(new Runnable() {
            @Override
            public void run() {
                while (isRun.get()) {
                    if (client.isConnect()) {
                        try {
                            //读取数据
                            client.onHandleResponse();
                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.d("====", "断开连接" + e.getMessage());
                            disconnectError();
            receiveThread.execute(new Runnable() {
                @Override
                public void run() {
                    while (isRun.get()) {
                        if (client.isConnect()) {
                            try {
                                //读取数据
                                client.onHandleResponse();
                            } catch (Exception e) {
                                e.printStackTrace();
                                disconnectError();
                            }
                        }
                    }
                    LogUtils.i("Socket 线程退出接收数据");
                }
            }
        });
            });
        }
    }
@@ -205,7 +213,7 @@
            @Override
            public void run() {
                if (isRun.get()) {
                    Log.d("====", "===发送心跳包===");
//                    LogUtils.d("====", "===发送心跳包===");
                    if (client.getOptions() != null) {
                        final byte[] heartBeat = client.getOptions().getHeartbeatData();
                        if (heartBeat != null) {
@@ -254,7 +262,6 @@
        if (!client.isConnect()) {
            resetConnect(false);
        }
    }
    /**
@@ -274,7 +281,6 @@
                    if (!client.isConnect() && isOpenRetry.get()) {
                        resetConnect(false);
                    }
                }
            }, 3000, TimeUnit.MILLISECONDS);
        }