From e45a95ead2ce5f78ef99b223a44991a112bf1432 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Sun, 24 May 2020 14:07:18 +0200 Subject: Attempt at fixing mixed content ws call --- .../kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt | 4 ++-- .../org/luxons/sevenwonders/client/SevenWondersClient.kt | 4 ++-- .../org/luxons/sevenwonders/server/SevenWondersTest.kt | 2 +- .../kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt | 13 +++++++++++-- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt b/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt index 9f5f2983..68d96e55 100644 --- a/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt +++ b/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt @@ -32,8 +32,8 @@ class SevenWondersBot( ) { private val client = SevenWondersClient() - suspend fun play(serverHost: String, gameId: Long) = withTimeout(botConfig.globalTimeout) { - val session = client.connect(serverHost) + suspend fun play(serverUrl: String, gameId: Long) = withTimeout(botConfig.globalTimeout) { + val session = client.connect(serverUrl) session.chooseName(displayName, Icon("desktop")) session.joinGame(gameId) session.awaitGameStart(gameId) diff --git a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt index 4185a6e4..4e3cf4b2 100644 --- a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt +++ b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt @@ -30,8 +30,8 @@ class SevenWondersClient { private val stompClient = StompClient() - suspend fun connect(serverHost: String): SevenWondersSession { - val session = stompClient.connect("ws://$serverHost$SEVEN_WONDERS_WS_ENDPOINT").withJsonConversions() + suspend fun connect(serverUrl: String): SevenWondersSession { + val session = stompClient.connect("$serverUrl$SEVEN_WONDERS_WS_ENDPOINT").withJsonConversions() return SevenWondersSession(session) } } diff --git a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/SevenWondersTest.kt b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/SevenWondersTest.kt index dfa55719..00922f73 100644 --- a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/SevenWondersTest.kt +++ b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/SevenWondersTest.kt @@ -22,7 +22,7 @@ class SevenWondersTest { private suspend fun connectNewClient(): SevenWondersSession { val client = SevenWondersClient() - val serverUrl = "localhost:$randomServerPort" + val serverUrl = "ws://localhost:$randomServerPort" return client.connect(serverUrl) } diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt index 5c426a51..7b56594c 100644 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt +++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt @@ -22,8 +22,8 @@ typealias SwSagaContext = SagaContext @OptIn(ExperimentalCoroutinesApi::class) suspend fun SwSagaContext.rootSaga() = coroutineScope { val action = next() - val serverHost = if (isProdEnv()) window.location.host else "localhost:8000" - val session = SevenWondersClient().connect(serverHost) + val serverUrl = sevenWondersWebSocketUrl() + val session = SevenWondersClient().connect(serverUrl) console.info("Connected to Seven Wonders web socket API") launch(start = CoroutineStart.UNDISPATCHED) { @@ -43,6 +43,15 @@ suspend fun SwSagaContext.rootSaga() = coroutineScope { } } +private fun sevenWondersWebSocketUrl(): String { + if (!isProdEnv()) { + return "ws://localhost:8000" + } + // prevents mixed content requests + val scheme = if (window.location.protocol.startsWith("https")) "wss" else "ws" + return "$scheme://${window.location.host}" +} + private suspend fun serverErrorSaga(session: SevenWondersSession) { session.watchErrors().collect { err -> // TODO use blueprintjs toaster -- cgit