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/ui/redux/sagas/Sagas.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'sw-ui/src/main/kotlin') 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