summaryrefslogtreecommitdiff
path: root/sw-server/src
diff options
context:
space:
mode:
authorjoffrey-bion <joffrey.bion@gmail.com>2020-11-27 14:35:41 +0100
committerjoffrey-bion <joffrey.bion@gmail.com>2020-11-27 14:38:45 +0100
commitd673a7ae1004a3ac8185e0ca35b6e25dfd7d9d84 (patch)
tree7952514104e3999ef0761033902c85528a2557c6 /sw-server/src
parentUpgrade to Spring Boot 2.4.0 (diff)
downloadseven-wonders-d673a7ae1004a3ac8185e0ca35b6e25dfd7d9d84.tar.gz
seven-wonders-d673a7ae1004a3ac8185e0ca35b6e25dfd7d9d84.tar.bz2
seven-wonders-d673a7ae1004a3ac8185e0ca35b6e25dfd7d9d84.zip
Use Kotlinx Serialization in Spring Boot instead of Jackson
Diffstat (limited to 'sw-server/src')
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSocketConfig.kt7
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt6
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt4
3 files changed, 14 insertions, 3 deletions
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSocketConfig.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSocketConfig.kt
index aad1c2ce..2782c670 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSocketConfig.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSocketConfig.kt
@@ -3,6 +3,8 @@ package org.luxons.sevenwonders.server.config
import org.luxons.sevenwonders.model.api.SEVEN_WONDERS_WS_ENDPOINT
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
+import org.springframework.messaging.converter.KotlinSerializationJsonMessageConverter
+import org.springframework.messaging.converter.MessageConverter
import org.springframework.messaging.simp.config.ChannelRegistration
import org.springframework.messaging.simp.config.MessageBrokerRegistry
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
@@ -48,4 +50,9 @@ class WebSocketConfig(
override fun configureClientInboundChannel(registration: ChannelRegistration) {
registration.interceptors(topicSubscriptionInterceptor)
}
+
+ override fun configureMessageConverters(messageConverters: MutableList<MessageConverter>): Boolean {
+ messageConverters.add(KotlinSerializationJsonMessageConverter())
+ return true
+ }
}
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt
index b63f6756..587afc10 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt
@@ -36,7 +36,7 @@ class GameBrowserController(
* @return the current list of [Lobby]s
*/
@SubscribeMapping("/games") // prefix /topic not shown
- fun listGames(principal: Principal): Collection<LobbyDTO> {
+ fun listGames(principal: Principal): List<LobbyDTO> {
logger.info("Player '{}' subscribed to /topic/games", principal.name)
return lobbyRepository.list().map { it.toDTO() }
}
@@ -61,7 +61,9 @@ class GameBrowserController(
// notify everyone that a new game exists
val lobbyDto = lobby.toDTO()
- template.convertAndSend("/topic/games", listOf(lobbyDto))
+ // we need to pass a non-generic type (array is fine) so that Spring doesn't break when trying to find a
+ // serializer from Kotlinx Serialization
+ template.convertAndSend("/topic/games", listOf(lobbyDto).toTypedArray())
return lobbyDto
}
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt
index fccb9b5b..7757c9d9 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt
@@ -113,7 +113,9 @@ class LobbyController(
lobby.getPlayers().forEach {
template.convertAndSendToUser(it.username, "/queue/lobby/updated", lobby.toDTO())
}
- template.convertAndSend("/topic/games", listOf(lobby.toDTO()))
+ // we need to pass a non-generic type (array is fine) so that Spring doesn't break when trying to find a
+ // serializer from Kotlinx Serialization
+ template.convertAndSend("/topic/games", listOf(lobby.toDTO()).toTypedArray())
}
/**
bgstack15