From b159fb479f1efbadc5fca5480c4fef1fd8cb0f55 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 12 Jan 2021 21:35:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96web=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/web/SourceDebugWebSocket.kt | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt b/app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt index 38d46fa6d..97a4ad0f1 100644 --- a/app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt +++ b/app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt @@ -22,12 +22,12 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) : override fun onOpen() { launch(IO) { - do { - delay(30000) - runCatching { + kotlin.runCatching { + while (isOpen) { ping("ping".toByteArray()) + delay(30000) } - } while (isOpen) + } } } @@ -42,19 +42,21 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) : override fun onMessage(message: NanoWSD.WebSocketFrame) { if (!message.textPayload.isJson()) return - kotlin.runCatching { - val debugBean = GSON.fromJsonObject>(message.textPayload) - if (debugBean != null) { - val tag = debugBean["tag"] - val key = debugBean["key"] - if (tag.isNullOrBlank() || key.isNullOrBlank()) { - send(App.INSTANCE.getString(R.string.cannot_empty)) - close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false) - return - } - App.db.bookSourceDao.getBookSource(tag)?.let { - Debug.callback = this - Debug.startDebug(this, WebBook(it), key) + launch(IO) { + kotlin.runCatching { + val debugBean = GSON.fromJsonObject>(message.textPayload) + if (debugBean != null) { + val tag = debugBean["tag"] + val key = debugBean["key"] + if (tag.isNullOrBlank() || key.isNullOrBlank()) { + send(App.INSTANCE.getString(R.string.cannot_empty)) + close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false) + return@launch + } + App.db.bookSourceDao.getBookSource(tag)?.let { + Debug.callback = this@SourceDebugWebSocket + Debug.startDebug(this, WebBook(it), key) + } } } } @@ -69,11 +71,15 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) : } override fun printLog(state: Int, msg: String) { - kotlin.runCatching { - send(msg) - if (state == -1 || state == 1000) { - Debug.cancelDebug(true) - close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false) + launch(IO) { + runCatching { + send(msg) + if (state == -1 || state == 1000) { + Debug.cancelDebug(true) + close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false) + } + }.onFailure { + it.printStackTrace() } } }