Osman Saral
03/24/2023, 4:54 PM@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.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.