create a FrameChannelConverter
This commit is contained in:
@@ -10,9 +10,8 @@ import eventDemo.app.event.GameEventHandler
|
||||
import eventDemo.app.event.event.GameEvent
|
||||
import eventDemo.app.event.projection.GameStateRepository
|
||||
import eventDemo.app.notification.ErrorNotification
|
||||
import eventDemo.shared.toFrame
|
||||
import eventDemo.app.notification.Notification
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.ktor.websocket.Frame
|
||||
import kotlinx.coroutines.channels.ReceiveChannel
|
||||
import kotlinx.coroutines.channels.SendChannel
|
||||
|
||||
@@ -32,8 +31,8 @@ class GameCommandHandler(
|
||||
*/
|
||||
suspend fun handle(
|
||||
player: Player,
|
||||
incomingCommandChannel: ReceiveChannel<Frame>,
|
||||
outgoingErrorChannelNotification: SendChannel<Frame>,
|
||||
incomingCommandChannel: ReceiveChannel<GameCommand>,
|
||||
outgoingErrorChannelNotification: SendChannel<Notification>,
|
||||
) = GameCommandStream(incomingCommandChannel).process { command ->
|
||||
if (command.payload.player.id != player.id) {
|
||||
nack()
|
||||
@@ -45,7 +44,7 @@ class GameCommandHandler(
|
||||
message = "Notification send ERROR: ${notification.message}"
|
||||
payload = mapOf("notification" to notification)
|
||||
}
|
||||
outgoingErrorChannelNotification.send(notification.toFrame())
|
||||
outgoingErrorChannelNotification.send(notification)
|
||||
}
|
||||
|
||||
val gameState = gameStateRepository.get(command.payload.gameId)
|
||||
|
||||
@@ -2,6 +2,8 @@ package eventDemo.app.command
|
||||
|
||||
import eventDemo.app.entity.Player
|
||||
import eventDemo.app.eventListener.GameEventPlayerNotificationListener
|
||||
import eventDemo.libs.fromFrameChannel
|
||||
import eventDemo.libs.toObjectChannel
|
||||
import io.ktor.server.application.ApplicationCall
|
||||
import io.ktor.server.auth.authenticate
|
||||
import io.ktor.server.auth.jwt.JWTPrincipal
|
||||
@@ -21,7 +23,11 @@ fun Route.gameSocket(
|
||||
webSocket("/game") {
|
||||
val currentPlayer = call.getPlayer()
|
||||
GlobalScope.launch {
|
||||
commandHandler.handle(currentPlayer, incoming, outgoing)
|
||||
commandHandler.handle(
|
||||
currentPlayer,
|
||||
toObjectChannel(incoming),
|
||||
fromFrameChannel(outgoing),
|
||||
)
|
||||
}
|
||||
playerNotificationListener.startListening(outgoing, currentPlayer)
|
||||
}
|
||||
|
||||
@@ -3,22 +3,11 @@ package eventDemo.app.command
|
||||
import eventDemo.app.command.command.GameCommand
|
||||
import eventDemo.libs.command.CommandStream
|
||||
import eventDemo.libs.command.CommandStreamChannel
|
||||
import eventDemo.libs.command.CommandStreamInMemory
|
||||
import io.ktor.websocket.Frame
|
||||
import kotlinx.coroutines.channels.ReceiveChannel
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
/**
|
||||
* A stream to publish and read the game command.
|
||||
*/
|
||||
class GameCommandStreamInMemory : CommandStreamInMemory<GameCommand>()
|
||||
|
||||
/**
|
||||
* A stream to publish and read the game command.
|
||||
*/
|
||||
class GameCommandStream(
|
||||
incoming: ReceiveChannel<Frame>,
|
||||
) : CommandStream<GameCommand> by CommandStreamChannel(
|
||||
incoming,
|
||||
{ Json.decodeFromString(GameCommand.serializer(), it) },
|
||||
)
|
||||
incoming: ReceiveChannel<GameCommand>,
|
||||
) : CommandStream<GameCommand> by CommandStreamChannel(incoming)
|
||||
|
||||
Reference in New Issue
Block a user