From df9083f10ffaf993bc23a3dcee248a0ad406aa68 Mon Sep 17 00:00:00 2001
From: liugl <liugl@sinata.cn>
Date: 星期六, 08 八月 2020 01:32:35 +0800
Subject: [PATCH] 8.7

---
 app/src/main/java/com/okgoincar/base/MyApplication.kt |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/app/src/main/java/com/okgoincar/base/MyApplication.kt b/app/src/main/java/com/okgoincar/base/MyApplication.kt
index 920f2b2..f1a0f7f 100644
--- a/app/src/main/java/com/okgoincar/base/MyApplication.kt
+++ b/app/src/main/java/com/okgoincar/base/MyApplication.kt
@@ -76,6 +76,9 @@
     }
 
     private fun initSocket() {
+        Handler(Looper.getMainLooper()).post {
+            toast("websocket初始化")
+        }
         NettyClient.getInstance().init(this, Api.SOCKET_SERVER, Api.SOCKET_PORT, true)
         NettyClient.getInstance()
             .addOnMessageListener { message: String? ->
@@ -85,9 +88,9 @@
                     currentTime = System.currentTimeMillis()
                     val json = JSONObject(message)
                     val method = json.optString("method")
-//                    Handler(Looper.getMainLooper()).post {
-//                        toast("收到websocket=$method")
-//                    }
+                    Handler(Looper.getMainLooper()).post {
+                        toast("收到websocket=$method")
+                    }
                     val data = json.optString("data")
                     val code = json.optInt("code", -1)
                     if (code == 200) {
@@ -159,6 +162,7 @@
             .setOnConnectListener {
                 sendHeart()
             }
+        sendHeart()
     }
 
     override fun attachBaseContext(base: Context?) {
@@ -266,7 +270,7 @@
         }
     }
 
-    private fun sendHeart() {
+    open fun sendHeart() {
         if (getUserId() != -1) {
             Log.i(TAG, "sendHeart: userId === " + getUserId())
             val bean = HeartBean()
@@ -282,7 +286,8 @@
             if ((System.currentTimeMillis() - currentTime) / 1000 > 30) {
                 sysErr("sendHeart_websocket 重新连接")
                 currentTime = System.currentTimeMillis()
-                NettyClient.getInstance().checkNettyState()
+//                NettyClient.getInstance().checkNettyState()
+                initSocket()
                 heartHandler!!.postDelayed({ sendHeart() }, 5000)
             } else {
                 sysErr("sendHeart_websocket 发送心跳")

--
Gitblit v1.7.1