Can anyone tell me what's wrong here? What am I do...
# ktor
o
Can anyone tell me what's wrong here? What am I doing wrong.
Copy code
@Test
    fun test() = testApplication {
        externalServices {
            hosts("<wss://test.com>") {
                install(io.ktor.server.websocket.WebSockets) { }

                routing {
                    webSocket("/test") {
                        send(Frame.Text("hello"))
                    }
                }
            }
        }

        val client = createClient {
            install(WebSockets) {
                contentConverter = KotlinxWebsocketSerializationConverter(
                    Json {
                        ignoreUnknownKeys = true
                    }
                )
            }
        }

        runBlocking {
            client.wss(host = "<http://test.com|test.com>", path = "test") {
                val string = Json.encodeToString(User("foo"))
//                     val string = """{"name":"foo"}"""
                println(string)
                send(Frame.Text(string))
            }
        }
    }
On the runBlocking block; both lines prints
{"name":"foo"}
but the`send` function throws
java.util.concurrent.CancellationException: ArrayChannel was cancelled
error for the first
string
, and it doesn't throw for the second.
closing the socket after a delay after
send(Frame.Text("hello"))
fixed the issue. This means server vas closed too soon. before encoding to string. since encodeToString takes some time, send is called after server is closed. this caused the issue.