This commit is contained in:
HapeLee committed 2025-06-04 13:33:51 +08:00
1 parent b86d90021c
commit 6c0757c91b
972 files changed
+6885 -7321

No files matched your search

@@ -0,0 +1,97 @@
package io.legato.kazusa.web.socket
import fi.iki.elonen.NanoHTTPD
import fi.iki.elonen.NanoWSD
import io.legato.kazusa.R
import io.legato.kazusa.data.entities.SearchBook
import io.legato.kazusa.help.config.AppConfig
import io.legato.kazusa.model.webBook.SearchModel
import io.legato.kazusa.ui.book.search.SearchScope
import io.legato.kazusa.utils.GSON
import io.legato.kazusa.utils.fromJsonObject
import io.legato.kazusa.utils.isJson
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import splitties.init.appCtx
import java.io.IOException
class BookSearchWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) :
NanoWSD.WebSocket(handshakeRequest),
CoroutineScope by MainScope(),
SearchModel.CallBack {
private val normalClosure = NanoWSD.WebSocketFrame.CloseCode.NormalClosure
private val searchModel = SearchModel(this, this)
private val SEARCH_FINISH = "Search finish"
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()
searchModel.close()
}
override fun onMessage(message: NanoWSD.WebSocketFrame) {
launch(IO) {
kotlin.runCatching {
if (!message.textPayload.isJson()) {
send("数据必须为Json格式")
close(normalClosure, SEARCH_FINISH, false)
return@launch
}
val searchMap =
GSON.fromJsonObject<Map<String, String>>(message.textPayload).getOrNull()
if (searchMap != null) {
val key = searchMap["key"]
if (key.isNullOrBlank()) {
send(appCtx.getString(R.string.cannot_empty))
close(normalClosure, SEARCH_FINISH, false)
return@launch
}
searchModel.search(System.currentTimeMillis(), key)
}
}
}
}
override fun onPong(pong: NanoWSD.WebSocketFrame) {
}
override fun onException(exception: IOException) {
}
override fun getSearchScope(): SearchScope = SearchScope(AppConfig.searchScope)
override fun onSearchStart() {
}
override fun onSearchSuccess(searchBooks: List<SearchBook>) {
send(GSON.toJson(searchBooks))
}
override fun onSearchFinish(isEmpty: Boolean, hasMore: Boolean) = close(normalClosure, SEARCH_FINISH, false)
override fun onSearchCancel(exception: Throwable?) = close(normalClosure, exception?.toString() ?: SEARCH_FINISH, false)
}
@@ -0,0 +1,102 @@
package io.legato.kazusa.web.socket
import fi.iki.elonen.NanoHTTPD
import fi.iki.elonen.NanoWSD
import io.legato.kazusa.R
import io.legato.kazusa.data.appDb
import io.legato.kazusa.model.Debug
import io.legato.kazusa.utils.*
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO
import splitties.init.appCtx
import java.io.IOException
/**
* web端书源调试
*/
class BookSourceDebugWebSocket(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<Map<String, String>>(message.textPayload).getOrNull()
if (debugBean != null) {
val tag = debugBean["tag"]
val key = debugBean["key"]
if (tag.isNullOrBlank() || key.isNullOrBlank()) {
send(appCtx.getString(R.string.cannot_empty))
close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false)
return@launch
}
appDb.bookSourceDao.getBookSource(tag)?.let {
Debug.callback = this@BookSourceDebugWebSocket
Debug.startDebug(this, it, key)
}
} else {
send("数据必须为Json格式")
close(NanoWSD.WebSocketFrame.CloseCode.NormalClosure, "调试结束", false)
return@launch
}
}
}
}
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()
}
}
}
}
@@ -0,0 +1,97 @@
package io.legato.kazusa.web.socket
import fi.iki.elonen.NanoHTTPD
import fi.iki.elonen.NanoWSD
import io.legato.kazusa.R
import io.legato.kazusa.data.appDb
import io.legato.kazusa.model.Debug
import io.legato.kazusa.utils.*
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO
import splitties.init.appCtx
import java.io.IOException
/**
* web端订阅源调试
*/
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<Map<String, String>>(message.textPayload).getOrNull()
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()
}
}
}
}