Kotlin Multiplatform Cell (KMM) WebSocket code not working for iOS


I am engaged on a Kotlin Multiplatform Cell (KMM) challenge the place I am utilizing WebSockets to ship and obtain messages. I’ve carried out a category WsChat for the WebSocket consumer, which works as anticipated on Android however not on iOS.

This is the code for WsChat:

import com.bokutotu.dateappmobile.mannequin.*
import io.ktor.consumer.*
import io.ktor.consumer.plugins.websocket.*
import io.ktor.http.*
import io.ktor.websocket.*
import kotlinx.coroutines.*
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

count on enjoyable wsClient(url: String, config: HttpClientConfig<*>.() -> Unit = {}): HttpClient

class WsChat(
    non-public val host: String = "127.0.0.1",
    non-public val port: Int = 8080,
    non-public val chatId: Int,
    non-public val userId: Int,
    non-public val initRecieve: (String) -> Unit,
    non-public val onReceive: (String) -> Unit
) {
    non-public val consumer = wsClient(host)
    non-public var session: DefaultClientWebSocketSession? = null
    non-public val sessionPromise = CompletableDeferred<DefaultClientWebSocketSession?>()
    non-public var initialMessageReceived = false
    non-public val scope = CoroutineScope(Dispatchers.IO)

    init {
        scope.launch {
            initSession()
        }
    }

    non-public droop enjoyable initSession() {
        consumer.webSocket(
            methodology = HttpMethod.Get,
            host = host,
            port = port,
            path = "/message/$chatId/$userId"
        ) {
            session = this
            sessionPromise.full(this)
            initReceivePipeline()
        }
    }

    non-public droop enjoyable initReceivePipeline() {
        val session = sessionPromise.await()
        session?.let {
            for (body in it.incoming) {
                frameHandler(body) { receivedText ->
                    if (!initialMessageReceived) {
                        initialMessageReceived = true
                        initRecieve(receivedText)
                    } else {
                        onReceive(receivedText)
                    }
                    println("right here")
                }
            }
        }
    }

    non-public enjoyable frameHandler(body: Body, receiveFunc: (String) -> Unit) {
        when (body) {
            is Body.Textual content -> {
                val receivedText = body.readText()
                receiveFunc(receivedText)
            }
            else -> println("Sudden Body format")
        }
    }

    enjoyable ship(newMessage: NewMessageJson) {
        val json = Json.encodeToString(newMessage)
        scope.launch {
            val session = sessionPromise.await()
            session?.ship(Body.Textual content(json))
        }
    }

    enjoyable shut() {
        scope.cancel()
        consumer.shut()
    }
}

This code works completely on the Android platform but it surely would not work on iOS. After I run the iOS app, the error is under.

> Job :shared:compileKotlinIosSimulatorArm64 FAILED
e: WsChat.kt:26:52 Unresolved reference: IO

I think that it could be associated to the totally different threading fashions in iOS, or some nuances in KMM or coroutine that I might not be conscious of. Does anybody have any thought why this could be taking place and the way I might resolve it?

Any assist could be appreciated. Thanks prematurely!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles