From c47d2016dfe2efb77043de0b03486c938263211c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 11 Feb 2024 20:09:52 +0100 Subject: [PATCH] Use WSS protocol when API base url is behind SSL --- .../data/repository/MessagesRepository.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/io/github/mklkj/kommunicator/data/repository/MessagesRepository.kt b/composeApp/src/commonMain/kotlin/io/github/mklkj/kommunicator/data/repository/MessagesRepository.kt index ab6825a..10965a1 100644 --- a/composeApp/src/commonMain/kotlin/io/github/mklkj/kommunicator/data/repository/MessagesRepository.kt +++ b/composeApp/src/commonMain/kotlin/io/github/mklkj/kommunicator/data/repository/MessagesRepository.kt @@ -14,9 +14,9 @@ import io.github.mklkj.kommunicator.data.models.MessageRequest import io.ktor.client.HttpClient import io.ktor.client.plugins.websocket.DefaultClientWebSocketSession import io.ktor.client.plugins.websocket.webSocketSession -import io.ktor.client.request.host -import io.ktor.client.request.port import io.ktor.http.URLBuilder +import io.ktor.http.URLProtocol +import io.ktor.http.path import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.emitAll import kotlinx.coroutines.flow.flow @@ -108,12 +108,14 @@ class MessagesRepository( } suspend fun getChatSession(chatId: UUID): DefaultClientWebSocketSession { - val session = httpClient.webSocketSession("/ws/chats/$chatId/messages") { - val url = URLBuilder(BuildKonfig.BASE_URL) - host = url.host - port = url.port + val url = (URLBuilder(BuildKonfig.BASE_URL)).apply { + protocol = when (protocol) { + URLProtocol.HTTPS -> URLProtocol.WSS + else -> URLProtocol.WS + } + path("/ws/chats/$chatId/messages") } - return session + return httpClient.webSocketSession(url.buildString()) } suspend fun handleReceivedMessage(chatId: UUID, message: MessageBroadcast) {