From 7804ce8edeba51418b5fde2d306e5f476dfb5993 Mon Sep 17 00:00:00 2001
From: 18034727885 <973609879@qq.com>
Date: 星期三, 18 三月 2026 10:03:22 +0800
Subject: [PATCH] 合并最新代码

---
 rxnetty/src/main/java/cn/sinata/rxnetty/CoreService.java |   63 +++++++++++++++++++++++--------
 1 files changed, 47 insertions(+), 16 deletions(-)

diff --git a/rxnetty/src/main/java/cn/sinata/rxnetty/CoreService.java b/rxnetty/src/main/java/cn/sinata/rxnetty/CoreService.java
index 423cca3..49e3361 100644
--- a/rxnetty/src/main/java/cn/sinata/rxnetty/CoreService.java
+++ b/rxnetty/src/main/java/cn/sinata/rxnetty/CoreService.java
@@ -1,5 +1,7 @@
 package cn.sinata.rxnetty;
 
+import static cn.sinata.rxnetty.LogHelper.logRecord;
+
 import android.app.*;
 import android.content.Context;
 import android.content.Intent;
@@ -7,7 +9,6 @@
 import android.graphics.Color;
 import android.os.Build;
 import android.os.IBinder;
-import android.util.Log;
 
 import androidx.annotation.Nullable;
 import androidx.core.app.NotificationCompat;
@@ -15,6 +16,7 @@
 import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.concurrent.TimeUnit;
+
 import cn.sinata.rxnetty.netStatus.NetChangeObserver;
 import cn.sinata.rxnetty.netStatus.NetStateReceiver;
 import cn.sinata.rxnetty.netStatus.NetUtils;
@@ -33,6 +35,8 @@
  */
 public class CoreService extends Service {
 
+    private String TAG = "CoreService";
+
     @Nullable
     @Override
     public IBinder onBind(Intent intent) {
@@ -42,6 +46,7 @@
     @Override
     public void onCreate() {
         super.onCreate();
+        logRecord(TAG, "onCreate");
     }
 
     void init() {
@@ -52,7 +57,10 @@
             @Override
             public void onSend(String s) {
                 //先检查通道状态,如果断开,则重连。
-                checkState();
+                boolean state = checkState();
+                if (!state) {
+                    reConnect();
+                }
                 Observable<Void> send = send(s + "\n");
 //                Observable<Void> send = send(s);
                 if (send != null) {
@@ -76,19 +84,27 @@
         initCheckOb();
     }
 
-    public void checkState() {
+    public boolean checkState() {
         if (mConnection == null || mConnection.getChannel() == null
                 || !mConnection.getChannel().isActive()
                 || !mConnection.getChannel().isWritable()) {
-            reConnect();
+            logRecord(TAG, "checkState need reConnect");
+            return false;
+        } else {
+            logRecord(TAG, "checkState no need reConnect");
+            return true;
         }
     }
 
     void initCheckOb() {
         NettyClient.getInstance().setOnCheckListener(new OnCheckListener() {
             @Override
-            public void doCheck() {
-                checkState();
+            public boolean doCheck() {
+                boolean state = checkState();
+                if (!state) {
+                    reConnect();
+                }
+                return state;
             }
         });
     }
@@ -105,21 +121,31 @@
             @Override
             public void onNetConnected(NetUtils.NetType type) {
                 super.onNetConnected(type);
-                checkState();
+                logRecord(TAG, "onNetConnected start checkState");
+                boolean state = checkState();
+                if (!state) {
+                    reConnect();
+                }
+            }
+
+            @Override
+            public void onNetDisConnect() {
+                super.onNetDisConnect();
+                logRecord(TAG, "onNetDisConnect");
             }
         };
         NetStateReceiver.registerObserver(mNetChangeObserver);
         if (Config.isStartForeground) {
             if (Build.VERSION.SDK_INT >= 34) {
-                startForeground(Config.NOTIFICATION_ID,getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING);
+                startForeground(Config.NOTIFICATION_ID, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING);
             } else {
-                startForeground(Config.NOTIFICATION_ID,getNotification());
+                startForeground(Config.NOTIFICATION_ID, getNotification());
             }
         }
         return START_NOT_STICKY;
     }
 
-    private Notification getNotification(){
+    private Notification getNotification() {
         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
             //8.0以上创建channel
             NotificationManager notificationManager =
@@ -147,9 +173,9 @@
         intent.setClassName(getPackageName(), getPackageName() + ".broadcast.MBroadcastReceiver");
         PendingIntent contentIntent;
         if (Build.VERSION.SDK_INT >= 31) {
-            contentIntent= PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
-        }else {
-            contentIntent= PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+            contentIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
+        } else {
+            contentIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
         }
         mBuilder.setContentIntent(contentIntent);
         return mBuilder.build();
@@ -161,6 +187,7 @@
     public void onDestroy() {
         super.onDestroy();
         isDestroy = true;
+        logRecord(TAG, "onDestroy");
         NetStateReceiver.unRegisterNetworkStateReceiver(this);
         if (subscriber != null && !subscriber.isUnsubscribed()) {
             subscriber.unsubscribe();
@@ -174,8 +201,9 @@
     }
 
     private void connectServer() {
-
-        if (NetUtils.isNetworkAvailable(this)) {
+        boolean status = NetUtils.isNetworkAvailable(this);
+        logRecord(TAG, "connectServer net status = " + status);
+        if (status) {
             connectionSub();
             connect(Config.SOCKET_SERVER, Config.SOCKET_PORT)
                     .subscribeOn(Schedulers.io())
@@ -202,6 +230,7 @@
             @Override
             public void onError(Throwable e) {
                 e.printStackTrace();
+                logRecord(TAG, "connectionSub onError " + e.getMessage());
                 reConnect();
             }
 
@@ -232,6 +261,7 @@
 
             @Override
             public void onError(Throwable e) {
+                logRecord(TAG, "initReceiveOb onError " + e.getMessage());
                 reConnect();
             }
 
@@ -257,6 +287,7 @@
 
 
     private void reConnect() {
+        logRecord(TAG, "reConnect isDestroy = " + isDestroy);
         if (isDestroy) {
             return;
         }
@@ -268,7 +299,7 @@
                     mConnection.close();
                     mConnection = null;
                 }
-                Log.i("CoreService", "sendHeart_websocket长连接断开,重新连接");
+                logRecord(TAG, "reConnect sendHeart_websocket长连接断开,重新连接");
                 connectServer();
             }
         });

--
Gitblit v1.7.1