mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-08-11 06:41:14 +00:00
Compare commits
17 Commits
0709b8f526
...
develop
Author | SHA1 | Date | |
---|---|---|---|
|
b2f449bf65 | ||
|
5e3a350e15 | ||
|
8a0a507e5b | ||
|
1c4b818a85 | ||
|
83b5eaf345 | ||
|
b0be81df20 | ||
|
a99f3b342a | ||
|
a9d3ad436b | ||
|
53757476a7 | ||
|
27810c0b7f | ||
|
7257100adc | ||
|
f29370a31f | ||
|
2c0c887ba1 | ||
|
5223cbe2b2 | ||
|
11f9895198 | ||
|
a18b83fcc8 | ||
|
30d5edc5fe |
@@ -11,6 +11,7 @@ import org.slf4j.Logger
|
|||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.chatbot.chzzk.Connector.getChannel
|
import space.mori.chzzk_bot.chatbot.chzzk.Connector.getChannel
|
||||||
import space.mori.chzzk_bot.chatbot.discord.Discord
|
import space.mori.chzzk_bot.chatbot.discord.Discord
|
||||||
|
import space.mori.chzzk_bot.chatbot.utils.refreshAccessToken
|
||||||
import space.mori.chzzk_bot.common.events.*
|
import space.mori.chzzk_bot.common.events.*
|
||||||
import space.mori.chzzk_bot.common.models.User
|
import space.mori.chzzk_bot.common.models.User
|
||||||
import space.mori.chzzk_bot.common.services.LiveStatusService
|
import space.mori.chzzk_bot.common.services.LiveStatusService
|
||||||
@@ -23,7 +24,7 @@ import xyz.r2turntrue.chzzk4j.session.ChzzkSessionSubscriptionType
|
|||||||
import xyz.r2turntrue.chzzk4j.session.ChzzkUserSession
|
import xyz.r2turntrue.chzzk4j.session.ChzzkUserSession
|
||||||
import xyz.r2turntrue.chzzk4j.session.event.SessionChatMessageEvent
|
import xyz.r2turntrue.chzzk4j.session.event.SessionChatMessageEvent
|
||||||
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
|
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
|
||||||
import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveStatus
|
import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveDetail
|
||||||
import java.lang.Exception
|
import java.lang.Exception
|
||||||
import java.net.SocketTimeoutException
|
import java.net.SocketTimeoutException
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
@@ -37,7 +38,7 @@ object ChzzkHandler {
|
|||||||
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
|
|
||||||
fun addUser(chzzkChannel: ChzzkChannel, user: User) {
|
fun addUser(chzzkChannel: ChzzkChannel, user: User) {
|
||||||
handlers.add(UserHandler(chzzkChannel, logger, user, streamStartTime = null))
|
handlers.add(UserHandler(chzzkChannel, logger, user, streamStartTime = LocalDateTime.now()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun enable() {
|
fun enable() {
|
||||||
@@ -196,6 +197,7 @@ object ChzzkHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OptIn(DelicateCoroutinesApi::class)
|
||||||
class UserHandler(
|
class UserHandler(
|
||||||
val channel: ChzzkChannel,
|
val channel: ChzzkChannel,
|
||||||
val logger: Logger,
|
val logger: Logger,
|
||||||
@@ -205,6 +207,7 @@ class UserHandler(
|
|||||||
var messageHandler: MessageHandler
|
var messageHandler: MessageHandler
|
||||||
var client: ChzzkClient
|
var client: ChzzkClient
|
||||||
var listener: ChzzkUserSession
|
var listener: ChzzkUserSession
|
||||||
|
var chatChannelId: String?
|
||||||
|
|
||||||
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
private var _isActive: Boolean
|
private var _isActive: Boolean
|
||||||
@@ -220,23 +223,39 @@ class UserHandler(
|
|||||||
throw RuntimeException("AccessToken or RefreshToken is not valid.")
|
throw RuntimeException("AccessToken or RefreshToken is not valid.")
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
val tokens = Connector.client.refreshAccessToken(user.refreshToken!!)
|
||||||
client = Connector.getClient(user.accessToken!!, user.refreshToken!!)
|
client = Connector.getClient(tokens.first, tokens.second)
|
||||||
|
UserService.setRefreshToken(user, tokens.first, tokens.second)
|
||||||
|
chatChannelId = getChzzkChannelId(channel.channelId)
|
||||||
|
|
||||||
client.loginAsync().join()
|
client.loginAsync().join()
|
||||||
client.refreshTokenAsync().join()
|
|
||||||
|
|
||||||
UserService.setRefreshToken(user, client.loginResult.accessToken(), client.loginResult.refreshToken())
|
|
||||||
|
|
||||||
listener = ChzzkSessionBuilder(client).buildUserSession()
|
listener = ChzzkSessionBuilder(client).buildUserSession()
|
||||||
|
|
||||||
listener.createAndConnectAsync().join()
|
listener.createAndConnectAsync().join()
|
||||||
|
|
||||||
messageHandler = MessageHandler(this@UserHandler)
|
messageHandler = MessageHandler(this@UserHandler)
|
||||||
|
|
||||||
listener.on(SessionChatMessageEvent::class.java) {
|
listener.on(SessionChatMessageEvent::class.java) {
|
||||||
messageHandler.handle(it.message, user)
|
messageHandler.handle(it.message, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GlobalScope.launch {
|
||||||
|
val timer = TimerConfigService.getConfig(user)
|
||||||
|
if (timer?.option == TimerType.UPTIME.value)
|
||||||
|
dispatcher.post(
|
||||||
|
TimerEvent(
|
||||||
|
channel.channelId,
|
||||||
|
TimerType.UPTIME,
|
||||||
|
getUptime(streamStartTime!!)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else dispatcher.post(
|
||||||
|
TimerEvent(
|
||||||
|
channel.channelId,
|
||||||
|
TimerType.entries.firstOrNull { it.value == timer?.option } ?: TimerType.REMOVE,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
} catch(e: Exception) {
|
} catch(e: Exception) {
|
||||||
logger.error("Exception(${user.username}): ${e.stackTraceToString()}")
|
logger.error("Exception(${user.username}): ${e.stackTraceToString()}")
|
||||||
throw RuntimeException("Exception: ${e.stackTraceToString()}")
|
throw RuntimeException("Exception: ${e.stackTraceToString()}")
|
||||||
@@ -259,7 +278,7 @@ class UserHandler(
|
|||||||
internal val isActive: Boolean
|
internal val isActive: Boolean
|
||||||
get() = _isActive
|
get() = _isActive
|
||||||
|
|
||||||
internal fun isActive(value: Boolean, status: ChzzkLiveStatus) {
|
internal fun isActive(value: Boolean, status: ChzzkLiveDetail) {
|
||||||
if(value) {
|
if(value) {
|
||||||
CoroutineScope(Dispatchers.Default).launch {
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
logger.info("${user.username} is live.")
|
logger.info("${user.username} is live.")
|
||||||
@@ -267,7 +286,7 @@ class UserHandler(
|
|||||||
reloadUser(UserService.getUser(user.id.value)!!)
|
reloadUser(UserService.getUser(user.id.value)!!)
|
||||||
|
|
||||||
logger.info("ChzzkChat connecting... ${channel.channelName} - ${channel.channelId}")
|
logger.info("ChzzkChat connecting... ${channel.channelName} - ${channel.channelId}")
|
||||||
listener.subscribeAsync(ChzzkSessionSubscriptionType.CHAT)
|
listener.subscribeAsync(ChzzkSessionSubscriptionType.CHAT).join()
|
||||||
|
|
||||||
streamStartTime = LocalDateTime.now()
|
streamStartTime = LocalDateTime.now()
|
||||||
|
|
||||||
|
@@ -14,7 +14,7 @@ import xyz.r2turntrue.chzzk4j.ChzzkClientBuilder
|
|||||||
import xyz.r2turntrue.chzzk4j.auth.ChzzkLegacyLoginAdapter
|
import xyz.r2turntrue.chzzk4j.auth.ChzzkLegacyLoginAdapter
|
||||||
import xyz.r2turntrue.chzzk4j.auth.ChzzkSimpleUserLoginAdapter
|
import xyz.r2turntrue.chzzk4j.auth.ChzzkSimpleUserLoginAdapter
|
||||||
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
|
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
|
||||||
import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveStatus
|
import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveDetail
|
||||||
import kotlin.getValue
|
import kotlin.getValue
|
||||||
|
|
||||||
val dotenv = dotenv {
|
val dotenv = dotenv {
|
||||||
@@ -31,7 +31,7 @@ object Connector {
|
|||||||
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
|
|
||||||
fun getChannel(channelId: String): ChzzkChannel? = client.fetchChannel(channelId)
|
fun getChannel(channelId: String): ChzzkChannel? = client.fetchChannel(channelId)
|
||||||
fun getLive(channelId: String): ChzzkLiveStatus? = client.fetchLiveStatus(channelId)
|
fun getLive(channelId: String): ChzzkLiveDetail? = client.fetchLiveDetail(channelId)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
logger.info("chzzk logged: ${client.isLoggedIn}")
|
logger.info("chzzk logged: ${client.isLoggedIn}")
|
||||||
|
@@ -88,7 +88,7 @@ class MessageHandler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun manageAddCommand(msg: SessionChatMessage, user: User) {
|
private fun manageAddCommand(msg: SessionChatMessage, user: User) {
|
||||||
if (msg.profile.badges.size == 0) {
|
if (msg.profile.badges.none { it.imageUrl.contains("manager") }) {
|
||||||
handler.sendChat("매니저만 명령어를 추가할 수 있습니다.")
|
handler.sendChat("매니저만 명령어를 추가할 수 있습니다.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -109,7 +109,7 @@ class MessageHandler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun manageUpdateCommand(msg: SessionChatMessage, user: User) {
|
private fun manageUpdateCommand(msg: SessionChatMessage, user: User) {
|
||||||
if (msg.profile.badges.size == 0) {
|
if (msg.profile.badges.none { it.imageUrl.contains("manager") }) {
|
||||||
handler.sendChat("매니저만 명령어를 추가할 수 있습니다.")
|
handler.sendChat("매니저만 명령어를 추가할 수 있습니다.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -131,7 +131,7 @@ class MessageHandler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun manageRemoveCommand(msg: SessionChatMessage, user: User) {
|
private fun manageRemoveCommand(msg: SessionChatMessage, user: User) {
|
||||||
if (msg.profile.badges.size == 0) {
|
if (msg.profile.badges.none { it.imageUrl.contains("manager") }) {
|
||||||
handler.sendChat("매니저만 명령어를 삭제할 수 있습니다.")
|
handler.sendChat("매니저만 명령어를 삭제할 수 있습니다.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -148,7 +148,7 @@ class MessageHandler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun timerCommand(msg: SessionChatMessage, user: User) {
|
private fun timerCommand(msg: SessionChatMessage, user: User) {
|
||||||
if (msg.profile.badges.size == 0) {
|
if (msg.profile.badges.none { it.imageUrl.contains("manager") }) {
|
||||||
handler.sendChat("매니저만 이 명령어를 사용할 수 있습니다.")
|
handler.sendChat("매니저만 이 명령어를 사용할 수 있습니다.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -227,7 +227,7 @@ class MessageHandler(
|
|||||||
|
|
||||||
val config = SongConfigService.getConfig(user)
|
val config = SongConfigService.getConfig(user)
|
||||||
|
|
||||||
if(config.streamerOnly && msg.profile.badges.size == 0) {
|
if(config.streamerOnly && msg.profile.badges.none { it.imageUrl.contains("manager") }) {
|
||||||
handler.sendChat("매니저만 이 명령어를 사용할 수 있습니다.")
|
handler.sendChat("매니저만 이 명령어를 사용할 수 있습니다.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -298,7 +298,7 @@ class MessageHandler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun songStartCommand(msg: SessionChatMessage, user: User) {
|
private fun songStartCommand(msg: SessionChatMessage, user: User) {
|
||||||
if (msg.profile?.badges?.size == 0) {
|
if (msg.profile.badges.none { it.imageUrl.contains("manager") }) {
|
||||||
handler.sendChat("매니저만 이 명령어를 사용할 수 있습니다.")
|
handler.sendChat("매니저만 이 명령어를 사용할 수 있습니다.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -353,15 +353,15 @@ class MessageHandler(
|
|||||||
// Replace followPattern
|
// Replace followPattern
|
||||||
result = followPattern.replace(result) { _ ->
|
result = followPattern.replace(result) { _ ->
|
||||||
try {
|
try {
|
||||||
val followingDate = getFollowDate(channel.channelId, msg.senderChannelId)
|
val followingDate = handler.chatChannelId?.let { getFollowDate(it, msg.senderChannelId) }
|
||||||
.content?.streamingProperty?.following?.followDate
|
?.content?.streamingProperty?.following?.followDate ?: LocalDateTime.now().minusDays(1).toString()
|
||||||
|
|
||||||
val period = followingDate?.let {
|
val period = followingDate.let {
|
||||||
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||||
val pastDate = LocalDateTime.parse(it, formatter)
|
val pastDate = LocalDateTime.parse(it, formatter)
|
||||||
val today = LocalDateTime.now()
|
val today = LocalDateTime.now()
|
||||||
ChronoUnit.DAYS.between(pastDate, today)
|
ChronoUnit.DAYS.between(pastDate, today)
|
||||||
} ?: 0
|
} + 1
|
||||||
|
|
||||||
period.toString()
|
period.toString()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
@@ -14,7 +14,8 @@ import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder
|
|||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.chatbot.discord.commands.*
|
import space.mori.chzzk_bot.chatbot.discord.commands.*
|
||||||
import space.mori.chzzk_bot.common.models.User
|
import space.mori.chzzk_bot.common.models.User
|
||||||
import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveStatus
|
import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveDetail
|
||||||
|
import xyz.r2turntrue.chzzk4j.types.channel.live.Resolution
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import kotlin.jvm.optionals.getOrNull
|
import kotlin.jvm.optionals.getOrNull
|
||||||
|
|
||||||
@@ -33,7 +34,7 @@ class Discord: ListenerAdapter() {
|
|||||||
return bot.getGuildById(guildId)?.getTextChannelById(channelId)
|
return bot.getGuildById(guildId)?.getTextChannelById(channelId)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendDiscord(user: User, status: ChzzkLiveStatus) {
|
fun sendDiscord(user: User, status: ChzzkLiveDetail) {
|
||||||
if(user.liveAlertMessage != null && user.liveAlertGuild != null && user.liveAlertChannel != null) {
|
if(user.liveAlertMessage != null && user.liveAlertGuild != null && user.liveAlertChannel != null) {
|
||||||
val channel = getChannel(user.liveAlertGuild ?: 0, user.liveAlertChannel ?: 0)
|
val channel = getChannel(user.liveAlertGuild ?: 0, user.liveAlertChannel ?: 0)
|
||||||
?: throw RuntimeException("${user.liveAlertChannel} is not valid.")
|
?: throw RuntimeException("${user.liveAlertChannel} is not valid.")
|
||||||
@@ -45,7 +46,14 @@ class Discord: ListenerAdapter() {
|
|||||||
embed.setAuthor(user.username, "https://chzzk.naver.com/live/${user.token}")
|
embed.setAuthor(user.username, "https://chzzk.naver.com/live/${user.token}")
|
||||||
embed.addField("카테고리", status.liveCategoryValue, true)
|
embed.addField("카테고리", status.liveCategoryValue, true)
|
||||||
embed.addField("태그", status.tags.joinToString(", ") { it.trim() }, true)
|
embed.addField("태그", status.tags.joinToString(", ") { it.trim() }, true)
|
||||||
// embed.setImage(status.)
|
status.defaultThumbnailImageUrl.getOrNull()?.let { embed.setImage(it) }
|
||||||
|
?: Resolution.entries.reversed().forEach {
|
||||||
|
val thumbnail = status.getLiveImageUrl(it)
|
||||||
|
if (thumbnail != null) {
|
||||||
|
embed.setImage(thumbnail)
|
||||||
|
return@forEach
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
channel.sendMessage(
|
channel.sendMessage(
|
||||||
MessageCreateBuilder()
|
MessageCreateBuilder()
|
||||||
|
@@ -0,0 +1,55 @@
|
|||||||
|
package space.mori.chzzk_bot.chatbot.utils
|
||||||
|
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
|
import space.mori.chzzk_bot.common.utils.client
|
||||||
|
import xyz.r2turntrue.chzzk4j.ChzzkClient
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
val client = OkHttpClient.Builder()
|
||||||
|
.addNetworkInterceptor { chain ->
|
||||||
|
chain.proceed(
|
||||||
|
chain.request()
|
||||||
|
.newBuilder()
|
||||||
|
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
|
||||||
|
.build()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
.build()
|
||||||
|
val gson = Gson()
|
||||||
|
|
||||||
|
data class RefreshTokenResponse(
|
||||||
|
val accessToken: String,
|
||||||
|
val refreshToken: String,
|
||||||
|
val expiresIn: Int,
|
||||||
|
val tokenType: String = "Bearer",
|
||||||
|
val scope: String
|
||||||
|
)
|
||||||
|
|
||||||
|
fun ChzzkClient.refreshAccessToken(refreshToken: String): Pair<String, String> {
|
||||||
|
val url = "https://chzzk.naver.com/auth/v1/token"
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.post(gson.toJson(mapOf(
|
||||||
|
"grantType" to "refresh_token",
|
||||||
|
"refreshToken" to refreshToken,
|
||||||
|
"clientId" to this.apiClientId,
|
||||||
|
"clientSecret" to this.apiSecret
|
||||||
|
)).toRequestBody())
|
||||||
|
.build()
|
||||||
|
|
||||||
|
client.newCall(request).execute().use { response ->
|
||||||
|
try {
|
||||||
|
if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}")
|
||||||
|
val body = response.body?.string()
|
||||||
|
val data = gson.fromJson(body, RefreshTokenResponse::class.java)
|
||||||
|
|
||||||
|
return Pair(data.accessToken, data.refreshToken)
|
||||||
|
} catch(e: Exception) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -52,6 +52,20 @@ data class NicknameColor(
|
|||||||
val colorCode: String = ""
|
val colorCode: String = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
data class LiveStatus(
|
||||||
|
val liveTitle: String,
|
||||||
|
val status: String,
|
||||||
|
val concurrentUserCount: Int,
|
||||||
|
val accumulateCount: Int,
|
||||||
|
val paidPromotion: Boolean,
|
||||||
|
val adult: Boolean,
|
||||||
|
val krOnlyViewing: Boolean,
|
||||||
|
val openDate: String,
|
||||||
|
val closeDate: String?,
|
||||||
|
val clipActive: Boolean,
|
||||||
|
val chatChannelId: String
|
||||||
|
)
|
||||||
|
|
||||||
// OkHttpClient에 Interceptor 추가
|
// OkHttpClient에 Interceptor 추가
|
||||||
val client = OkHttpClient.Builder()
|
val client = OkHttpClient.Builder()
|
||||||
.addNetworkInterceptor { chain ->
|
.addNetworkInterceptor { chain ->
|
||||||
@@ -84,3 +98,24 @@ fun getFollowDate(chatID: String, userId: String) : IData<IFollowContent?> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getChzzkChannelId(channelId: String): String? {
|
||||||
|
val url = "https://api.chzzk.naver.com/polling/v3/channels/$channelId/live-status?includePlayerRecommendContent=false"
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.get()
|
||||||
|
.build()
|
||||||
|
|
||||||
|
client.newCall(request).execute().use { response ->
|
||||||
|
try {
|
||||||
|
if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}")
|
||||||
|
val body = response.body?.string()
|
||||||
|
val data = gson.fromJson(body, object: TypeToken<IData<LiveStatus?>>() {})
|
||||||
|
|
||||||
|
return data.content?.chatChannelId
|
||||||
|
} catch(e: Exception) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -37,9 +37,10 @@ fun Routing.wsSongRoutes() {
|
|||||||
fun addSession(uid: String, session: WebSocketServerSession) {
|
fun addSession(uid: String, session: WebSocketServerSession) {
|
||||||
sessions.computeIfAbsent(uid) { ConcurrentLinkedQueue() }.add(session)
|
sessions.computeIfAbsent(uid) { ConcurrentLinkedQueue() }.add(session)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun removeSession(uid: String, session: WebSocketServerSession) {
|
fun removeSession(uid: String, session: WebSocketServerSession) {
|
||||||
sessions[uid]?.remove(session)
|
sessions[uid]?.remove(session)
|
||||||
if(sessions[uid]?.isEmpty() == true) {
|
if (sessions[uid]?.isEmpty() == true) {
|
||||||
sessions.remove(uid)
|
sessions.remove(uid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,30 +89,50 @@ fun Routing.wsSongRoutes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
webSocket("/song/{uid}") {
|
webSocket("/song/{uid}") {
|
||||||
|
logger.info("WebSocket connection attempt received")
|
||||||
val uid = call.parameters["uid"]
|
val uid = call.parameters["uid"]
|
||||||
val user = uid?.let { UserService.getUser(it) }
|
val user = uid?.let { UserService.getUser(it) }
|
||||||
if (uid == null || user == null) {
|
if (uid == null || user == null) {
|
||||||
|
logger.warn("Invalid UID: $uid")
|
||||||
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid UID"))
|
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid UID"))
|
||||||
return@webSocket
|
return@webSocket
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
addSession(uid, this)
|
addSession(uid, this)
|
||||||
if(status[uid] == SongType.STREAM_OFF) {
|
logger.info("WebSocket connection established for user: $uid")
|
||||||
|
|
||||||
|
// Start heartbeat
|
||||||
|
val heartbeatJob = songScope.launch {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
send(Frame.Ping(ByteArray(0)))
|
||||||
|
delay(30000) // 30 seconds
|
||||||
|
} catch (e: Exception) {
|
||||||
|
logger.error("Heartbeat failed for user $uid", e)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status[uid] == SongType.STREAM_OFF) {
|
||||||
songScope.launch {
|
songScope.launch {
|
||||||
sendSerialized(SongResponse(
|
sendSerialized(
|
||||||
|
SongResponse(
|
||||||
SongType.STREAM_OFF.value,
|
SongType.STREAM_OFF.value,
|
||||||
uid,
|
uid,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
))
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
for (frame in incoming) {
|
for (frame in incoming) {
|
||||||
when(frame) {
|
when (frame) {
|
||||||
is Frame.Text -> {
|
is Frame.Text -> {
|
||||||
val text = frame.readText().trim()
|
val text = frame.readText().trim()
|
||||||
if(text == "ping") {
|
if (text == "ping") {
|
||||||
send("pong")
|
send("pong")
|
||||||
} else {
|
} else {
|
||||||
val data = Json.decodeFromString<SongRequest>(text)
|
val data = Json.decodeFromString<SongRequest>(text)
|
||||||
@@ -121,45 +142,58 @@ fun Routing.wsSongRoutes() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is Frame.Ping -> send(Frame.Pong(frame.data))
|
is Frame.Ping -> send(Frame.Pong(frame.data))
|
||||||
else -> {}
|
else -> {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(e: ClosedReceiveChannelException) {
|
} catch (e: ClosedReceiveChannelException) {
|
||||||
logger.error("Error in WebSocket: ${e.message}")
|
logger.error("WebSocket connection closed for user $uid: ${e.message}")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
logger.error("Unexpected error in WebSocket for user $uid", e)
|
||||||
} finally {
|
} finally {
|
||||||
|
logger.info("Cleaning up WebSocket connection for user $uid")
|
||||||
removeSession(uid, this)
|
removeSession(uid, this)
|
||||||
ackMap[uid]?.remove(this)
|
ackMap[uid]?.remove(this)
|
||||||
|
heartbeatJob.cancel()
|
||||||
|
}
|
||||||
|
} catch(e: Exception) {
|
||||||
|
logger.error("Unexpected error in WebSocket for user $uid", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
songScope.launch {
|
songScope.launch {
|
||||||
broadcastMessage(it.uid, SongResponse(
|
broadcastMessage(
|
||||||
|
it.uid, SongResponse(
|
||||||
it.type.value,
|
it.type.value,
|
||||||
it.uid,
|
it.uid,
|
||||||
it.reqUid,
|
it.reqUid,
|
||||||
it.current?.toSerializable(),
|
it.current?.toSerializable(),
|
||||||
it.next?.toSerializable(),
|
it.next?.toSerializable(),
|
||||||
it.delUrl
|
it.delUrl
|
||||||
))
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dispatcher.subscribe(TimerEvent::class) {
|
dispatcher.subscribe(TimerEvent::class) {
|
||||||
if(it.type == TimerType.STREAM_OFF) {
|
if (it.type == TimerType.STREAM_OFF) {
|
||||||
songScope.launch {
|
songScope.launch {
|
||||||
broadcastMessage(it.uid, SongResponse(
|
broadcastMessage(
|
||||||
|
it.uid, SongResponse(
|
||||||
it.type.value,
|
it.type.value,
|
||||||
it.uid,
|
it.uid,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
))
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class SerializableYoutubeVideo(
|
data class SerializableYoutubeVideo(
|
||||||
val url: String,
|
val url: String,
|
||||||
|
Reference in New Issue
Block a user