https://kotlinlang.org logo
Title
s

Sourabh Rawat

12/08/2020, 12:37 PM
fun main() = runBlocking {

    val httpClient = HttpClient {
        install(Logging) {
            level = LogLevel.ALL
        }

        @OptIn(KtorExperimentalAPI::class)
        install(WebSockets)

        expectSuccess = false

        install(JsonFeature) {
            @OptIn(KtorExperimentalAPI::class)
            acceptContentTypes = listOf(ContentType("application", "json-rpc"))
            serializer = KotlinxSerializer()
        }
    }
    val wss =httpClient.webSocketSession(method = HttpMethod.Get, host = "127.0.0.1", port = 6800, path = "/jsonrpc")
    wss.close()
}
this does not stop the program. what am I doing wrong?
shouldn't it just stop the program after
wss.close()
?
s

Saul Wiggin

12/08/2020, 1:12 PM
wws.close()
closes the websocket. If you want to cancel the coroutine to stop the program then you could use
main.cancel()
👀 1
s

Sourabh Rawat

12/08/2020, 1:16 PM
I mean, I am not asking
wss.close
to close the coruotune. What I want is that the main finishes which doesn't if I use this wss snippet. If I replace all wss code with just
println, then main finishes
j

Joris PZ

12/08/2020, 2:15 PM
Are you sure it's actually connecting at all? The API documentation is practically non-existent, but I think the call to
websocketSession
suspends until the connection is established, and so
main
wouldn't finish until it does
b

Big Chungus

12/08/2020, 3:04 PM
you need to close httpClient as well 🙂
Also, instead of doing
fun main() = runBlocking{}
you can do
suspend fun main() {}
s

Sourabh Rawat

12/08/2020, 3:13 PM
thats the fix i needed. thanks. Also, was
suspend mani
recent feature?
b

Big Chungus

12/08/2020, 6:17 PM
I've discovered it a month or so ago. No idea when it was actually introduced.
a

andylamax

12/09/2020, 12:33 AM
wait what? main suspend functions are a thing?