优化web调试

pull/801/head
gedoor 4 years ago
parent a76d64f040
commit b159fb479f
  1. 20
      app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt

@ -22,12 +22,12 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) :
override fun onOpen() { override fun onOpen() {
launch(IO) { launch(IO) {
do { kotlin.runCatching {
delay(30000) while (isOpen) {
runCatching {
ping("ping".toByteArray()) ping("ping".toByteArray())
delay(30000)
}
} }
} while (isOpen)
} }
} }
@ -42,6 +42,7 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) :
override fun onMessage(message: NanoWSD.WebSocketFrame) { override fun onMessage(message: NanoWSD.WebSocketFrame) {
if (!message.textPayload.isJson()) return if (!message.textPayload.isJson()) return
launch(IO) {
kotlin.runCatching { kotlin.runCatching {
val debugBean = GSON.fromJsonObject<Map<String, String>>(message.textPayload) val debugBean = GSON.fromJsonObject<Map<String, String>>(message.textPayload)
if (debugBean != null) { if (debugBean != null) {
@ -50,15 +51,16 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) :
if (tag.isNullOrBlank() || key.isNullOrBlank()) { if (tag.isNullOrBlank() || key.isNullOrBlank()) {
send(App.INSTANCE.getString(R.string.cannot_empty)) send(App.INSTANCE.getString(R.string.cannot_empty))
close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false) close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false)
return return@launch
} }
App.db.bookSourceDao.getBookSource(tag)?.let { App.db.bookSourceDao.getBookSource(tag)?.let {
Debug.callback = this Debug.callback = this@SourceDebugWebSocket
Debug.startDebug(this, WebBook(it), key) Debug.startDebug(this, WebBook(it), key)
} }
} }
} }
} }
}
override fun onPong(pong: NanoWSD.WebSocketFrame) { override fun onPong(pong: NanoWSD.WebSocketFrame) {
@ -69,12 +71,16 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) :
} }
override fun printLog(state: Int, msg: String) { override fun printLog(state: Int, msg: String) {
kotlin.runCatching { launch(IO) {
runCatching {
send(msg) send(msg)
if (state == -1 || state == 1000) { if (state == -1 || state == 1000) {
Debug.cancelDebug(true) Debug.cancelDebug(true)
close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false) close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false)
} }
}.onFailure {
it.printStackTrace()
}
} }
} }

Loading…
Cancel
Save