mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
Merge pull request #116 from dalbodeule/develop
some fix on wsTimerRoutes
This commit is contained in:
commit
bf01af04c6
@ -13,12 +13,12 @@ import org.slf4j.LoggerFactory
|
|||||||
import space.mori.chzzk_bot.common.events.*
|
import space.mori.chzzk_bot.common.events.*
|
||||||
import space.mori.chzzk_bot.common.services.TimerConfigService
|
import space.mori.chzzk_bot.common.services.TimerConfigService
|
||||||
import space.mori.chzzk_bot.common.services.UserService
|
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.ConcurrentHashMap
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue
|
import java.util.concurrent.ConcurrentLinkedQueue
|
||||||
|
|
||||||
fun Routing.wsTimerRoutes() {
|
fun Routing.wsTimerRoutes() {
|
||||||
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
|
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
|
||||||
val status = ConcurrentHashMap<String, TimerType>()
|
|
||||||
val logger = LoggerFactory.getLogger("WSTimerRoutes")
|
val logger = LoggerFactory.getLogger("WSTimerRoutes")
|
||||||
|
|
||||||
fun addSession(uid: String, session: WebSocketServerSession) {
|
fun addSession(uid: String, session: WebSocketServerSession) {
|
||||||
@ -45,17 +45,29 @@ fun Routing.wsTimerRoutes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addSession(uid, this)
|
addSession(uid, this)
|
||||||
|
val timer = CurrentTimer.getTimer(user)
|
||||||
|
|
||||||
if(status[uid] == TimerType.STREAM_OFF) {
|
if(timer?.type == TimerType.STREAM_OFF) {
|
||||||
CoroutineScope(Dispatchers.Default).launch {
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
sendSerialized(TimerResponse(TimerType.STREAM_OFF.value, null))
|
sendSerialized(TimerResponse(TimerType.STREAM_OFF.value, null))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
CoroutineScope(Dispatchers.Default).launch {
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
sendSerialized(TimerResponse(
|
if (timer == null) {
|
||||||
|
sendSerialized(
|
||||||
|
TimerResponse(
|
||||||
TimerConfigService.getConfig(user)?.option ?: TimerType.REMOVE.value,
|
TimerConfigService.getConfig(user)?.option ?: TimerType.REMOVE.value,
|
||||||
null
|
null
|
||||||
))
|
)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
sendSerialized(
|
||||||
|
TimerResponse(
|
||||||
|
timer.type.value,
|
||||||
|
timer.time
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +96,8 @@ fun Routing.wsTimerRoutes() {
|
|||||||
|
|
||||||
dispatcher.subscribe(TimerEvent::class) {
|
dispatcher.subscribe(TimerEvent::class) {
|
||||||
logger.debug("TimerEvent: {} / {}", it.uid, it.type)
|
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 {
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
sessions[it.uid]?.forEach { ws ->
|
sessions[it.uid]?.forEach { ws ->
|
||||||
ws.sendSerialized(TimerResponse(it.type.value, it.time ?: ""))
|
ws.sendSerialized(TimerResponse(it.type.value, it.time ?: ""))
|
||||||
|
@ -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 ?: ""]
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user