| | |
| | |
|
| | | 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;
|
| | |
| | | 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()) {
|
| | |
| | | try {
|
| | | resendCount.set(resendCount.get() + 1);
|
| | | Thread.sleep(300L);
|
| | | Log.d("====", "==重连第" + resendCount + "次==");
|
| | | LogUtils.d("====", "==重连第" + resendCount + "次==");
|
| | | } catch (Exception ignored) {
|
| | | }
|
| | | }
|
| | |
| | | client.connect();
|
| | | isRun.set(true);
|
| | | if (client.isConnect()) {
|
| | | Log.d("====", "====连接成功====");
|
| | | LogUtils.d("====", "====连接成功====");
|
| | | startHeartbeat();
|
| | |
|
| | | initSendThread();
|
| | |
| | | }
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | Log.d("====", "===连接失败===" + e);
|
| | | LogUtils.d("====", "===连接失败===" + e);
|
| | | //再判断一下有没有连接
|
| | | if (!client.isConnect()) {
|
| | | isRun.set(false);
|
| | |
| | | public void run() {
|
| | | while (isRun.get()) {
|
| | | if (client.isConnect()) {
|
| | | Log.d("=====", "==发送数据==");
|
| | | LogUtils.d("=====", "==发送数据==");
|
| | |
|
| | | try {
|
| | | SocketRequest socketRequest = mMessageQueue.take();
|
| | | final String sendStr = new String(socketRequest.getData(), 0, socketRequest.getData().length);
|
| | | Log.d("=====", "==发送数据==:"+sendStr);
|
| | | LogUtils.d("=====", "==发送数据==:"+sendStr);
|
| | | final String action = socketRequest.getAction();
|
| | | try {
|
| | | client.sendMsg(socketRequest.getData());
|
| | |
| | | }
|
| | |
|
| | | }
|
| | | Log.d("=====", "==发送线程关闭==");
|
| | | LogUtils.d("=====", "==发送线程关闭==");
|
| | | }
|
| | | });
|
| | |
|
| | |
| | | client.onHandleResponse();
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | Log.d("====", "断开连接" + e.getMessage());
|
| | | LogUtils.d("====", "断开连接" + e.getMessage());
|
| | | disconnectError();
|
| | | }
|
| | | }
|
| | |
| | | @Override
|
| | | public void run() {
|
| | | if (isRun.get()) {
|
| | | Log.d("====", "===发送心跳包===");
|
| | | LogUtils.d("====", "===发送心跳包===");
|
| | | if (client.getOptions() != null) {
|
| | | final byte[] heartBeat = client.getOptions().getHeartbeatData();
|
| | | if (heartBeat != null) {
|