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

Azur Haljeta

11/23/2020, 9:30 AM
Is there any websocket ktor client example, in which the entry object is available so it can be used to cancel or start the websocket session. So far, there is that example where you manage the websocket session within a scoped block of code and it isn't available to the outside world.
Copy code
httpClient.wss(
    method = HttpMethod.Get,
    host = "<http://echo.websocket.org|echo.websocket.org>",
    port = 8090,
) {
    // this is of 'DefaultClientWebSocketSession' type
    val frame = incoming.receive()
    when (frame) {
        is Frame.Text -> println(frame.readText())
        is Frame.Binary -> println(frame.readBytes())
    }
}
but what I need as reference, like this:
Copy code
val someWsObject = httpClient.wss(
    method = HttpMethod.Get,
    host = "<http://echo.websocket.org|echo.websocket.org>",
    port = 8090,
)

someWsObject.connect()

val frame = someWsObject.incoming.receive()
    when (frame) {
        is Frame.Text -> println(frame.readText())
        is Frame.Binary -> println(frame.readBytes())
    }
j

Joris PZ

11/23/2020, 4:19 PM
Is this what you are looking for?
e

edenman

11/23/2020, 7:39 PM
i just have a
private var webSocketSession: DefaultClientWebSocketSession? = null
and I do
webSocketSession = this
first thing inside my
block
it’s janky API but it works fine
a

Azur Haljeta

11/24/2020, 7:37 AM
thanks!! yes, these are viable alternatives but still overcomplicated I'll stick with okhttp3 websockets until this is fixed
👍 3
b

bbaldino

11/28/2020, 11:36 PM
I ran into this as well, and used the
HttpClient#webSocketSession
method which returns a
DefaultClientWebSocketSession
. There's a
close
method on that which can be used to stop it, and I defined an extension method on
DefaultClientWebSocketSession
in my class to start/run the message handling loop.
👍 1
164 Views