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

View File

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