some fix WSSongListRoutes.kt

- delete websocket key
- else updated.
This commit is contained in:
dalbodeule
2024-08-05 14:38:16 +09:00
parent cc23ac03e7
commit 75da1c1576
5 changed files with 34 additions and 10 deletions

View File

@@ -5,6 +5,7 @@ import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.Serializable
import space.mori.chzzk_bot.common.services.SongConfigService
import space.mori.chzzk_bot.common.utils.getStreamInfo
@Serializable
@@ -54,4 +55,31 @@ fun Routing.apiRoutes() {
call.respondText("Require UID", status = HttpStatusCode.NotFound)
}
}
route("/session/{sid}") {
get {
val sid = call.parameters["sid"]
if(sid == null) {
call.respondText("Require SID", status = HttpStatusCode.NotFound)
return@get
}
val user = SongConfigService.getUserByToken(sid)
if(user == null) {
call.respondText("User not found", status = HttpStatusCode.NotFound)
return@get
} else {
val chzzkUser = getStreamInfo(user.token)
call.respond(HttpStatusCode.OK, GetUserDTO(
chzzkUser.content!!.channel.channelId,
chzzkUser.content!!.channel.channelName,
chzzkUser.content!!.status == "OPEN",
chzzkUser.content!!.channel.channelImageUrl
))
}
}
}
route("/session") {
get {
call.respondText("Require SID", status = HttpStatusCode.NotFound)
}
}
}

View File

@@ -39,14 +39,13 @@ fun Routing.wsSongListRoutes() {
webSocket("/songlist/{sid}") {
val sid = call.parameters["sid"]
val pw = call.request.headers["X-Auth-Token"]
val session = sid?.let { SongConfigService.getConfig(it) }
val user = sid?.let {SongConfigService.getUserByToken(sid) }
if (sid == null) {
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid SID"))
return@webSocket
}
if (user == null || session == null || session.password != pw) {
if (user == null || session == null) {
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid SID"))
return@webSocket
}
@@ -149,6 +148,8 @@ fun Routing.wsSongListRoutes() {
}
dispatcher.subscribe(TimerEvent::class) {
if(it.type == TimerType.STREAM_OFF) {
val user = UserService.getUser(it.uid)
SongConfigService.updateSession(user!!, null)
CoroutineScope(Dispatchers.Default).launch {
sessions[it.uid]?.forEach { ws ->
ws.sendSerialized(SongResponse(