Shamrock: アクティブWebSocketハートビートの修復

This commit is contained in:
WhiteChi 2023-11-25 11:29:06 +08:00
parent 679b7619ce
commit c758b1576d
2 changed files with 26 additions and 19 deletions

View File

@ -24,8 +24,8 @@ import java.net.URI
internal class WebSocketService( internal class WebSocketService(
host: String, host: String,
port: Int, port: Int,
val heartbeatInterval: Long, heartbeatInterval: Long,
): WebSocketTransmitServlet(host, port) { ): WebSocketTransmitServlet(host, port, heartbeatInterval) {
private val eventJobList = mutableSetOf<Job>() private val eventJobList = mutableSetOf<Job>()
override fun submitFlowJob(job: Job) { override fun submitFlowJob(job: Job) {
@ -85,7 +85,6 @@ internal class WebSocketService(
} }
private fun pushMetaLifecycle() { private fun pushMetaLifecycle() {
if (heartbeatInterval <= 0) return
GlobalScope.launch { GlobalScope.launch {
val runtime = AppRuntimeFetcher.appRuntime val runtime = AppRuntimeFetcher.appRuntime
pushTo(PushMetaEvent( pushTo(PushMetaEvent(

View File

@ -35,7 +35,8 @@ import kotlin.concurrent.timer
internal abstract class WebSocketTransmitServlet( internal abstract class WebSocketTransmitServlet(
host:String, host:String,
port: Int port: Int,
protected val heartbeatInterval: Long,
) : BaseTransmitServlet, WebSocketServer(InetSocketAddress(host, port)) { ) : BaseTransmitServlet, WebSocketServer(InetSocketAddress(host, port)) {
private val sendLock = Mutex() private val sendLock = Mutex()
protected val eventReceivers: MutableList<WebSocket> = Collections.synchronizedList(mutableListOf<WebSocket>()) protected val eventReceivers: MutableList<WebSocket> = Collections.synchronizedList(mutableListOf<WebSocket>())
@ -56,20 +57,27 @@ internal abstract class WebSocketTransmitServlet(
} }
init { init {
timer("heartbeat", true, 0, 1000L * 5) { if (heartbeatInterval > 0) {
val runtime = AppRuntimeFetcher.appRuntime timer("heartbeat", true, 0, heartbeatInterval) {
val curUin = runtime.currentAccountUin val runtime = AppRuntimeFetcher.appRuntime
broadcastAnyEvent(PushMetaEvent( val curUin = runtime.currentAccountUin
time = System.currentTimeMillis() / 1000, broadcastAnyEvent(
selfId = app.longAccountUin, PushMetaEvent(
postType = PostType.Meta, time = System.currentTimeMillis() / 1000,
type = MetaEventType.Heartbeat, selfId = app.longAccountUin,
subType = MetaSubType.Connect, postType = PostType.Meta,
status = BotStatus( type = MetaEventType.Heartbeat,
Self("qq", curUin.toLong()), runtime.isLogin, status = "正常", good = true subType = MetaSubType.Connect,
), status = BotStatus(
interval = 15000 Self("qq", curUin.toLong()),
)) runtime.isLogin,
status = "正常",
good = true
),
interval = heartbeatInterval
)
)
}
} }
} }
@ -112,7 +120,7 @@ internal abstract class WebSocketTransmitServlet(
} }
override fun onStart() { override fun onStart() {
LogCenter.log("WSServer start running on ws://0.0.0.0:$port!") LogCenter.log("WSServer start running on ws://${getAddress()}!")
initTransmitter() initTransmitter()
} }