diff --git a/app/src/main/assets/web/bookSource/index.js b/app/src/main/assets/web/bookSource/index.js index 3b7b274e3..da166e3c5 100644 --- a/app/src/main/assets/web/bookSource/index.js +++ b/app/src/main/assets/web/bookSource/index.js @@ -353,7 +353,7 @@ $('.menu').addEventListener('click', e => { if (sResult.isSuccess) { let sKey = DebugKey.value ? DebugKey.value : '我的'; $('#DebugConsole').value = `源《${saveRule[0].bookSourceName}》保存成功!使用搜索关键字“${sKey}”开始调试...`; - let ws = new WebSocket(`${wsOrigin}/sourceDebug`); + let ws = new WebSocket(`${wsOrigin}/bookSourceDebug`); ws.onopen = () => { ws.send(`{"tag":"${saveRule[0].bookSourceUrl}", "key":"${sKey}"}`); }; diff --git a/app/src/main/assets/web/rssSource/index.js b/app/src/main/assets/web/rssSource/index.js index 68fb20dc9..ad0d02711 100644 --- a/app/src/main/assets/web/rssSource/index.js +++ b/app/src/main/assets/web/rssSource/index.js @@ -240,7 +240,7 @@ $('.menu').addEventListener('click', e => { if (sResult.isSuccess) { let sKey = DebugKey.value ? DebugKey.value : '我的'; $('#DebugConsole').value = `源《${saveRule[0].sourceName}》保存成功!使用搜索关键字“${sKey}”开始调试...`; - let ws = new WebSocket(`${wsOrigin}/sourceDebug`); + let ws = new WebSocket(`${wsOrigin}/rssSourceDebug`); ws.onopen = () => { ws.send(`{"tag":"${saveRule[0].sourceUrl}", "key":"${sKey}"}`); }; diff --git a/app/src/main/java/io/legado/app/web/WebSocketServer.kt b/app/src/main/java/io/legado/app/web/WebSocketServer.kt index d6a259687..45fd04bd4 100644 --- a/app/src/main/java/io/legado/app/web/WebSocketServer.kt +++ b/app/src/main/java/io/legado/app/web/WebSocketServer.kt @@ -1,12 +1,20 @@ package io.legado.app.web import fi.iki.elonen.NanoWSD +import io.legado.app.web.socket.BookSourceDebugWebSocket +import io.legado.app.web.socket.RssSourceDebugWebSocket class WebSocketServer(port: Int) : NanoWSD(port) { override fun openWebSocket(handshake: IHTTPSession): WebSocket? { - return if (handshake.uri == "/sourceDebug") { - SourceDebugWebSocket(handshake) - } else null + return when (handshake.uri) { + "/bookSourceDebug" -> { + BookSourceDebugWebSocket(handshake) + } + "/rssSourceDebug" -> { + RssSourceDebugWebSocket(handshake) + } + else -> null + } } } diff --git a/app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt b/app/src/main/java/io/legado/app/web/socket/BookSourceDebugWebSocket.kt similarity index 93% rename from app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt rename to app/src/main/java/io/legado/app/web/socket/BookSourceDebugWebSocket.kt index 7bc56202d..4bf0ef35f 100644 --- a/app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt +++ b/app/src/main/java/io/legado/app/web/socket/BookSourceDebugWebSocket.kt @@ -1,4 +1,4 @@ -package io.legado.app.web +package io.legado.app.web.socket import fi.iki.elonen.NanoHTTPD @@ -13,7 +13,7 @@ import splitties.init.appCtx import java.io.IOException -class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) : +class BookSourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) : NanoWSD.WebSocket(handshakeRequest), CoroutineScope by MainScope(), Debug.Callback { @@ -58,7 +58,7 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) : return@launch } appDb.bookSourceDao.getBookSource(tag)?.let { - Debug.callback = this@SourceDebugWebSocket + Debug.callback = this@BookSourceDebugWebSocket Debug.startDebug(this, it, key) } } diff --git a/app/src/main/java/io/legado/app/web/socket/RssSourceDebugWebSocket.kt b/app/src/main/java/io/legado/app/web/socket/RssSourceDebugWebSocket.kt new file mode 100644 index 000000000..f99c0a7e2 --- /dev/null +++ b/app/src/main/java/io/legado/app/web/socket/RssSourceDebugWebSocket.kt @@ -0,0 +1,93 @@ +package io.legado.app.web.socket + + +import fi.iki.elonen.NanoHTTPD +import fi.iki.elonen.NanoWSD +import io.legado.app.R +import io.legado.app.data.appDb +import io.legado.app.model.Debug +import io.legado.app.utils.* +import kotlinx.coroutines.* +import kotlinx.coroutines.Dispatchers.IO +import splitties.init.appCtx +import java.io.IOException + + +class RssSourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) : + NanoWSD.WebSocket(handshakeRequest), + CoroutineScope by MainScope(), + Debug.Callback { + + private val notPrintState = arrayOf(10, 20, 30, 40) + + override fun onOpen() { + launch(IO) { + kotlin.runCatching { + while (isOpen) { + ping("ping".toByteArray()) + delay(30000) + } + } + } + } + + override fun onClose( + code: NanoWSD.WebSocketFrame.CloseCode, + reason: String, + initiatedByRemote: Boolean + ) { + cancel() + Debug.cancelDebug(true) + } + + override fun onMessage(message: NanoWSD.WebSocketFrame) { + launch(IO) { + kotlin.runCatching { + if (!message.textPayload.isJson()) { + send("数据必须为Json格式") + close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false) + return@launch + } + val debugBean = GSON.fromJsonObject>(message.textPayload) + if (debugBean != null) { + val tag = debugBean["tag"] + if (tag.isNullOrBlank()) { + send(appCtx.getString(R.string.cannot_empty)) + close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false) + return@launch + } + appDb.rssSourceDao.getByKey(tag)?.let { + Debug.callback = this@RssSourceDebugWebSocket + Debug.startDebug(this, it) + } + } + } + } + } + + override fun onPong(pong: NanoWSD.WebSocketFrame) { + + } + + override fun onException(exception: IOException) { + Debug.cancelDebug(true) + } + + override fun printLog(state: Int, msg: String) { + if (state in notPrintState) { + return + } + runOnIO { + runCatching { + send(msg) + if (state == -1 || state == 1000) { + Debug.cancelDebug(true) + close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false) + } + }.onFailure { + it.printOnDebug() + } + } + } + +}