mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-08 23:08:20 +00:00
Refactor: replace songListScope with appropriate scopes
Replaced `songListScope` with `songScope` in `WSSongRoutes` and `timerScope` in `WSTimerRoutes` to better reflect their respective purposes. Improves code clarity and consistency in scope usage.
This commit is contained in:
parent
aa95976005
commit
61a5f985c1
@ -26,7 +26,7 @@ import java.util.concurrent.ConcurrentLinkedQueue
|
|||||||
val songScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
val songScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||||
fun Routing.wsSongRoutes() {
|
fun Routing.wsSongRoutes() {
|
||||||
environment.monitor.subscribe(ApplicationStopped) {
|
environment.monitor.subscribe(ApplicationStopped) {
|
||||||
songListScope.cancel()
|
songScope.cancel()
|
||||||
}
|
}
|
||||||
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
|
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
|
||||||
val status = ConcurrentHashMap<String, SongType>()
|
val status = ConcurrentHashMap<String, SongType>()
|
||||||
@ -77,7 +77,7 @@ fun Routing.wsSongRoutes() {
|
|||||||
fun broadcastMessage(userId: String, message: SongResponse) {
|
fun broadcastMessage(userId: String, message: SongResponse) {
|
||||||
val userSessions = sessions[userId]
|
val userSessions = sessions[userId]
|
||||||
userSessions?.forEach { session ->
|
userSessions?.forEach { session ->
|
||||||
songListScope.launch {
|
songScope.launch {
|
||||||
val success = sendWithRetry(session, message)
|
val success = sendWithRetry(session, message)
|
||||||
if (!success) {
|
if (!success) {
|
||||||
logger.info("Removing session for user $userId due to repeated failures.")
|
logger.info("Removing session for user $userId due to repeated failures.")
|
||||||
@ -96,7 +96,7 @@ fun Routing.wsSongRoutes() {
|
|||||||
}
|
}
|
||||||
addSession(uid, this)
|
addSession(uid, this)
|
||||||
if(status[uid] == SongType.STREAM_OFF) {
|
if(status[uid] == SongType.STREAM_OFF) {
|
||||||
songListScope.launch {
|
songScope.launch {
|
||||||
sendSerialized(SongResponse(
|
sendSerialized(SongResponse(
|
||||||
SongType.STREAM_OFF.value,
|
SongType.STREAM_OFF.value,
|
||||||
uid,
|
uid,
|
||||||
@ -135,7 +135,7 @@ fun Routing.wsSongRoutes() {
|
|||||||
|
|
||||||
dispatcher.subscribe(SongEvent::class) {
|
dispatcher.subscribe(SongEvent::class) {
|
||||||
logger.debug("SongEvent: {} / {} {}", it.uid, it.type, it.current?.name)
|
logger.debug("SongEvent: {} / {} {}", it.uid, it.type, it.current?.name)
|
||||||
songListScope.launch {
|
songScope.launch {
|
||||||
broadcastMessage(it.uid, SongResponse(
|
broadcastMessage(it.uid, SongResponse(
|
||||||
it.type.value,
|
it.type.value,
|
||||||
it.uid,
|
it.uid,
|
||||||
@ -148,7 +148,7 @@ fun Routing.wsSongRoutes() {
|
|||||||
}
|
}
|
||||||
dispatcher.subscribe(TimerEvent::class) {
|
dispatcher.subscribe(TimerEvent::class) {
|
||||||
if(it.type == TimerType.STREAM_OFF) {
|
if(it.type == TimerType.STREAM_OFF) {
|
||||||
songListScope.launch {
|
songScope.launch {
|
||||||
broadcastMessage(it.uid, SongResponse(
|
broadcastMessage(it.uid, SongResponse(
|
||||||
it.type.value,
|
it.type.value,
|
||||||
it.uid,
|
it.uid,
|
||||||
|
@ -27,7 +27,7 @@ import java.util.concurrent.ConcurrentLinkedQueue
|
|||||||
val timerScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
val timerScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||||
fun Routing.wsTimerRoutes() {
|
fun Routing.wsTimerRoutes() {
|
||||||
environment.monitor.subscribe(ApplicationStopped) {
|
environment.monitor.subscribe(ApplicationStopped) {
|
||||||
songListScope.cancel()
|
timerScope.cancel()
|
||||||
}
|
}
|
||||||
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
|
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
|
||||||
val logger = LoggerFactory.getLogger("WSTimerRoutes")
|
val logger = LoggerFactory.getLogger("WSTimerRoutes")
|
||||||
@ -77,7 +77,7 @@ fun Routing.wsTimerRoutes() {
|
|||||||
fun broadcastMessage(uid: String, message: TimerResponse) {
|
fun broadcastMessage(uid: String, message: TimerResponse) {
|
||||||
val userSessions = sessions[uid]
|
val userSessions = sessions[uid]
|
||||||
userSessions?.forEach { session ->
|
userSessions?.forEach { session ->
|
||||||
songListScope.launch {
|
timerScope.launch {
|
||||||
val success = sendWithRetry(session, message.copy(uid = uid))
|
val success = sendWithRetry(session, message.copy(uid = uid))
|
||||||
if (!success) {
|
if (!success) {
|
||||||
logger.info("Removing session for user $uid due to repeated failures.")
|
logger.info("Removing session for user $uid due to repeated failures.")
|
||||||
@ -98,11 +98,11 @@ fun Routing.wsTimerRoutes() {
|
|||||||
val timer = CurrentTimer.getTimer(user)
|
val timer = CurrentTimer.getTimer(user)
|
||||||
|
|
||||||
if (timer?.type == TimerType.STREAM_OFF) {
|
if (timer?.type == TimerType.STREAM_OFF) {
|
||||||
songListScope.launch {
|
timerScope.launch {
|
||||||
sendSerialized(TimerResponse(TimerType.STREAM_OFF.value, null, uid))
|
sendSerialized(TimerResponse(TimerType.STREAM_OFF.value, null, uid))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
songListScope.launch {
|
timerScope.launch {
|
||||||
if(timer?.type == TimerType.STREAM_OFF) {
|
if(timer?.type == TimerType.STREAM_OFF) {
|
||||||
sendSerialized(TimerResponse(TimerType.STREAM_OFF.value, null, uid))
|
sendSerialized(TimerResponse(TimerType.STREAM_OFF.value, null, uid))
|
||||||
} else {
|
} else {
|
||||||
@ -157,7 +157,7 @@ fun Routing.wsTimerRoutes() {
|
|||||||
logger.debug("TimerEvent: {} / {}", it.uid, it.type)
|
logger.debug("TimerEvent: {} / {}", it.uid, it.type)
|
||||||
val user = UserService.getUser(it.uid)
|
val user = UserService.getUser(it.uid)
|
||||||
CurrentTimer.setTimer(user!!, it)
|
CurrentTimer.setTimer(user!!, it)
|
||||||
songListScope.launch {
|
timerScope.launch {
|
||||||
broadcastMessage(it.uid, TimerResponse(it.type.value, it.time ?: "", it.uid))
|
broadcastMessage(it.uid, TimerResponse(it.type.value, it.time ?: "", it.uid))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user