Shamrock: wsハートビート間隔の制御をサポート

This commit is contained in:
WhiteChi 2023-11-25 00:42:10 +08:00
parent 282233131a
commit 679b7619ce
6 changed files with 24 additions and 14 deletions

View File

@ -158,7 +158,6 @@ fun LabFragment() {
}.onFailure {
AppRuntime.log("无法启用免死金牌选项当前Lsposed模块未激活或者不支持NewSharedPreferences。", Level.WARN)
}
}
}

View File

@ -14,8 +14,9 @@ import moe.fuqiuluo.shamrock.remote.service.api.GlobalEventTransmitter
internal class WebSocketClientService(
override val address: String,
heartbeatInterval: Long,
wsHeaders: Map<String, String>
) : WebSocketClientServlet(address, wsHeaders) {
) : WebSocketClientServlet(address, heartbeatInterval, wsHeaders) {
private val eventJobList = mutableSetOf<Job>()
override fun submitFlowJob(job: Job) {

View File

@ -21,7 +21,11 @@ import org.java_websocket.WebSocket
import org.java_websocket.handshake.ClientHandshake
import java.net.URI
internal class WebSocketService(host: String, port: Int): WebSocketTransmitServlet(host, port) {
internal class WebSocketService(
host: String,
port: Int,
val heartbeatInterval: Long,
): WebSocketTransmitServlet(host, port) {
private val eventJobList = mutableSetOf<Job>()
override fun submitFlowJob(job: Job) {
@ -81,9 +85,9 @@ internal class WebSocketService(host: String, port: Int): WebSocketTransmitServl
}
private fun pushMetaLifecycle() {
if (heartbeatInterval <= 0) return
GlobalScope.launch {
val runtime = AppRuntimeFetcher.appRuntime
val curUin = runtime.currentAccountUin
pushTo(PushMetaEvent(
time = System.currentTimeMillis() / 1000,
selfId = app.longAccountUin,
@ -91,9 +95,9 @@ internal class WebSocketService(host: String, port: Int): WebSocketTransmitServl
type = MetaEventType.LifeCycle,
subType = MetaSubType.Connect,
status = BotStatus(
Self("qq", curUin.toLong()), runtime.isLogin, status = "正常", good = true
Self("qq", runtime.longAccountUin), runtime.isLogin, status = "正常", good = true
),
interval = 15000
interval = heartbeatInterval
))
}
}

View File

@ -36,6 +36,7 @@ import kotlin.concurrent.timer
internal abstract class WebSocketClientServlet(
url: String,
private val heartbeatInterval: Long,
wsHeaders: Map<String, String>
) : BaseTransmitServlet, WebSocketClient(URI(url), wsHeaders) {
private val sendLock = Mutex()
@ -105,17 +106,17 @@ internal abstract class WebSocketClientServlet(
}
private fun startHeartbeatTimer() {
if (heartbeatInterval <= 0) return
timer(
name = "heartbeat",
initialDelay = 0,
period = 1000L * 15,
period = heartbeatInterval,
) {
if (isClosed || isClosing || !isOpen) {
cancel()
return@timer
}
val runtime = AppRuntimeFetcher.appRuntime
val curUin = runtime.currentAccountUin
send(
GlobalJson.encodeToString(
PushMetaEvent(
@ -125,7 +126,7 @@ internal abstract class WebSocketClientServlet(
type = MetaEventType.Heartbeat,
subType = MetaSubType.Connect,
status = BotStatus(
Self("qq", curUin.toLong()),
Self("qq", runtime.longAccountUin),
runtime.isLogin,
status = "正常",
good = true
@ -151,7 +152,7 @@ internal abstract class WebSocketClientServlet(
status = BotStatus(
Self("qq", curUin.toLong()), runtime.isLogin, status = "正常", good = true
),
interval = 15000
interval = heartbeatInterval
)
)
}

View File

@ -18,6 +18,7 @@ data class ConnectionConfig(
@SerialName("address") val address: String? = null,
@SerialName("port") var port: Int? = null,
@SerialName("token") val token: String? = null,
@SerialName("heartbeat_interval") var heartbeatInterval: Long? = null,
)
@Serializable

View File

@ -61,7 +61,7 @@ internal class InitRemoteService : IAction {
wsHeaders["authorization"] = "bearer $token"
}
LogCenter.log("尝试链接WebSocketClient(url = ${conn.address})",Level.WARN)
startWebSocketClient(conn.address, wsHeaders)
startWebSocketClient(conn.address, conn.heartbeatInterval ?: 15000, wsHeaders)
}
}
}
@ -80,7 +80,7 @@ internal class InitRemoteService : IAction {
return@launch
}
require(config.port in 0 .. 65536) { "WebSocketServer端口不合法" }
val server = WebSocketService(config.address, config.port!!)
val server = WebSocketService(config.address, config.port!!, config.heartbeatInterval ?: (15 * 1000))
server.isReuseAddr = true
server.start()
} catch (e: Throwable) {
@ -89,11 +89,15 @@ internal class InitRemoteService : IAction {
}
}
private fun startWebSocketClient(url: String, wsHeaders: HashMap<String, String>) {
private fun startWebSocketClient(
url: String,
interval: Long,
wsHeaders: HashMap<String, String>
) {
GlobalScope.launch {
try {
if (url.startsWith("ws://") || url.startsWith("wss://")) {
val wsClient = WebSocketClientService(url, wsHeaders)
val wsClient = WebSocketClientService(url, interval, wsHeaders)
wsClient.connect()
timer(initialDelay = 5000L, period = 5000L) {
if (wsClient.isClosed || wsClient.isClosing) {