some fix on wsTimerRoutes

This commit is contained in:
dalbodeule 2024-08-28 10:42:41 +09:00
parent 1186f647d2
commit 3806b0f824
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
2 changed files with 39 additions and 7 deletions

View File

@ -13,12 +13,12 @@ import org.slf4j.LoggerFactory
import space.mori.chzzk_bot.common.events.*
import space.mori.chzzk_bot.common.services.TimerConfigService
import space.mori.chzzk_bot.common.services.UserService
import space.mori.chzzk_bot.webserver.utils.CurrentTimer
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedQueue
fun Routing.wsTimerRoutes() {
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
val status = ConcurrentHashMap<String, TimerType>()
val logger = LoggerFactory.getLogger("WSTimerRoutes")
fun addSession(uid: String, session: WebSocketServerSession) {
@ -45,17 +45,29 @@ fun Routing.wsTimerRoutes() {
}
addSession(uid, this)
val timer = CurrentTimer.getTimer(user)
if(status[uid] == TimerType.STREAM_OFF) {
if(timer?.type == TimerType.STREAM_OFF) {
CoroutineScope(Dispatchers.Default).launch {
sendSerialized(TimerResponse(TimerType.STREAM_OFF.value, null))
}
} else {
CoroutineScope(Dispatchers.Default).launch {
sendSerialized(TimerResponse(
if (timer == null) {
sendSerialized(
TimerResponse(
TimerConfigService.getConfig(user)?.option ?: TimerType.REMOVE.value,
null
))
)
)
} else {
sendSerialized(
TimerResponse(
timer.type.value,
timer.time
)
)
}
}
}
@ -84,7 +96,8 @@ fun Routing.wsTimerRoutes() {
dispatcher.subscribe(TimerEvent::class) {
logger.debug("TimerEvent: {} / {}", it.uid, it.type)
status[it.uid] = it.type
val user = UserService.getUser(it.uid)
CurrentTimer.setTimer(user!!, it)
CoroutineScope(Dispatchers.Default).launch {
sessions[it.uid]?.forEach { ws ->
ws.sendSerialized(TimerResponse(it.type.value, it.time ?: ""))

View File

@ -0,0 +1,19 @@
package space.mori.chzzk_bot.webserver.utils
import space.mori.chzzk_bot.common.events.TimerEvent
import space.mori.chzzk_bot.common.models.User
import java.util.concurrent.ConcurrentHashMap
object CurrentTimer {
private val currentTimer = ConcurrentHashMap<String, TimerEvent>()
fun setTimer(user: User, timer: TimerEvent?) {
if(timer == null) {
currentTimer.remove(user.token ?: "")
} else {
currentTimer[user.token ?: ""] = timer
}
}
fun getTimer(user: User) = currentTimer[user.token ?: ""]
}