Skip to content

Alex009/ktor-client-ios-websockets

Repository files navigation

Proof of concept for idea of using ktor-client on iOS with WebSockets by wrapping of IosClientEngine.

Sample app connect to websockets on both platforms using ktor-client WebSockets feature:

val httpClient = HttpClient(createHttpClientEngine()) {
    install(WebSockets)
}
GlobalScope.launch(Dispatchers.Main) {
    log("try connect websocket")

    httpClient.webSocket("wss://echo.websocket.org") {
        log("connected websocket")

        val incomingJob = launch {
            incoming.consumeEach { frame ->
                log(frame.toString())

                if (frame is Frame.Text) {
                    val text: String = frame.readText()
                    log("received $text")

                    outgoing.send(Frame.Text(">$text"))
                    log("send response")
                }
            }
        }
        send("Hello world!")
        log("send first message")

        incomingJob.join()
        log("incoming job end")
    }

    log("websocket closed")
}

On android side it works out of box with OkHttpEngine. For iOS was implemented HttpClientEngine wrapper - WSIosHttpClientEngine. To create this engine we should pass another engine that will execute normal http requests:

WSIosHttpClientEngine(wrappedEngine = Ios.create { })

For details see classes: WSIosHttpClientEngine, IosWebSocket.

Plans

Implementation will be moved into moko-network - icerockdev/moko-network#149

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published