https://kotlinlang.org logo
#ktor
Title
# ktor
m

Marcin Bak

08/24/2020, 12:29 AM
Trying out ktor websockets on Android. Maybe someone encountered a case I have: the sending of messages works however when I try to receive messages I get an exception
Copy code
java.io.EOFException
        at okio.RealBufferedSource.require(RealBufferedSource.kt:201)
        at okio.RealBufferedSource.readByte(RealBufferedSource.kt:210)
        at okhttp3.internal.ws.WebSocketReader.readHeader(WebSocketReader.kt:119)
        at okhttp3.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.kt:102)
        at okhttp3.internal.ws.RealWebSocket.loopReader(RealWebSocket.kt:293)
        at okhttp3.internal.ws.RealWebSocket$connect$1.onResponse(RealWebSocket.kt:195)
        at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:504)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
my setup is simple
Copy code
val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
                implementation(project(":backend-frontend-shared"))
                implementation("io.ktor:ktor-client-core:$ktor_version")
                implementation("io.ktor:ktor-client-websockets:$ktor_version")
                implementation("io.ktor:ktor-client-serialization:$ktor_version")
                implementation("io.ktor:ktor-client-logging:$ktor_version")
            }
        }
        val androidMain by getting {
            dependencies {
                implementation(kotlin("stdlib"))
                implementation("io.ktor:ktor-client-websockets-jvm:$ktor_version")
                implementation("io.ktor:ktor-client-serialization-jvm:$ktor_version")
                implementation("io.ktor:ktor-client-okhttp:$ktor_version")
                implementation("io.ktor:ktor-client-logging-jvm:$ktor_version")
            }
        }
and I read from websocket like this:
Copy code
HttpClient {
            install(WebSockets)
        }.ws(
            method = HttpMethod.Get,
            host = "192.168.0.38",
            port = 8080,
            path = CHAT_WS_PATH
        ) {
            try {
                val frame = incoming.receive()
            } catch (e: Exception) {
                //" Here's the crash
            }
        }
most probably this is a bug in Android implementation of websockets using OkHttp engine…
16 Views