Files
event-demo/src/main/kotlin/eventDemo/contexts/game/infrastructure/websocket/GameCommandRouteWebSocket.kt
T

27 lines
978 B
Kotlin

package eventDemo.contexts.game.infrastructure.websocket
import eventDemo.contexts.game.application.channels.GameChannelsSubscriber
import eventDemo.contexts.game.domain.game.GameId
import eventDemo.libs.helpers.fromFrameChannel
import eventDemo.libs.helpers.toObjectChannel
import eventDemo.sharedKernel.currentUserId
import io.ktor.server.auth.authenticate
import io.ktor.server.routing.Route
import io.ktor.server.websocket.webSocket
import kotlinx.coroutines.DelicateCoroutinesApi
import java.util.UUID
@DelicateCoroutinesApi
fun Route.gameWebSocket(channelSubscriber: GameChannelsSubscriber) {
authenticate {
webSocket("/games/{id}") {
channelSubscriber.subscribePlayerToGameChannels(
gameId = GameId(UUID.nameUUIDFromBytes(call.parameters["id"]?.encodeToByteArray()!!)),
userId = call.currentUserId,
incomingCommandChannel = toObjectChannel(incoming),
sendNotificationChannel = fromFrameChannel(outgoing),
)
}
}
}