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 { }.onFailure {
AppRuntime.log("无法启用免死金牌选项当前Lsposed模块未激活或者不支持NewSharedPreferences。", Level.WARN) AppRuntime.log("无法启用免死金牌选项当前Lsposed模块未激活或者不支持NewSharedPreferences。", Level.WARN)
} }
} }
} }

View File

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

View File

@ -21,7 +21,11 @@ import org.java_websocket.WebSocket
import org.java_websocket.handshake.ClientHandshake import org.java_websocket.handshake.ClientHandshake
import java.net.URI 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>() private val eventJobList = mutableSetOf<Job>()
override fun submitFlowJob(job: Job) { override fun submitFlowJob(job: Job) {
@ -81,9 +85,9 @@ internal class WebSocketService(host: String, port: Int): WebSocketTransmitServl
} }
private fun pushMetaLifecycle() { private fun pushMetaLifecycle() {
if (heartbeatInterval <= 0) return
GlobalScope.launch { GlobalScope.launch {
val runtime = AppRuntimeFetcher.appRuntime val runtime = AppRuntimeFetcher.appRuntime
val curUin = runtime.currentAccountUin
pushTo(PushMetaEvent( pushTo(PushMetaEvent(
time = System.currentTimeMillis() / 1000, time = System.currentTimeMillis() / 1000,
selfId = app.longAccountUin, selfId = app.longAccountUin,
@ -91,9 +95,9 @@ internal class WebSocketService(host: String, port: Int): WebSocketTransmitServl
type = MetaEventType.LifeCycle, type = MetaEventType.LifeCycle,
subType = MetaSubType.Connect, subType = MetaSubType.Connect,
status = BotStatus( 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( internal abstract class WebSocketClientServlet(
url: String, url: String,
private val heartbeatInterval: Long,
wsHeaders: Map<String, String> wsHeaders: Map<String, String>
) : BaseTransmitServlet, WebSocketClient(URI(url), wsHeaders) { ) : BaseTransmitServlet, WebSocketClient(URI(url), wsHeaders) {
private val sendLock = Mutex() private val sendLock = Mutex()
@ -105,17 +106,17 @@ internal abstract class WebSocketClientServlet(
} }
private fun startHeartbeatTimer() { private fun startHeartbeatTimer() {
if (heartbeatInterval <= 0) return
timer( timer(
name = "heartbeat", name = "heartbeat",
initialDelay = 0, initialDelay = 0,
period = 1000L * 15, period = heartbeatInterval,
) { ) {
if (isClosed || isClosing || !isOpen) { if (isClosed || isClosing || !isOpen) {
cancel() cancel()
return@timer return@timer
} }
val runtime = AppRuntimeFetcher.appRuntime val runtime = AppRuntimeFetcher.appRuntime
val curUin = runtime.currentAccountUin
send( send(
GlobalJson.encodeToString( GlobalJson.encodeToString(
PushMetaEvent( PushMetaEvent(
@ -125,7 +126,7 @@ internal abstract class WebSocketClientServlet(
type = MetaEventType.Heartbeat, type = MetaEventType.Heartbeat,
subType = MetaSubType.Connect, subType = MetaSubType.Connect,
status = BotStatus( status = BotStatus(
Self("qq", curUin.toLong()), Self("qq", runtime.longAccountUin),
runtime.isLogin, runtime.isLogin,
status = "正常", status = "正常",
good = true good = true
@ -151,7 +152,7 @@ internal abstract class WebSocketClientServlet(
status = BotStatus( status = BotStatus(
Self("qq", curUin.toLong()), runtime.isLogin, status = "正常", good = true 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("address") val address: String? = null,
@SerialName("port") var port: Int? = null, @SerialName("port") var port: Int? = null,
@SerialName("token") val token: String? = null, @SerialName("token") val token: String? = null,
@SerialName("heartbeat_interval") var heartbeatInterval: Long? = null,
) )
@Serializable @Serializable

View File

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