Is there any websocket ktor client example, in whi...
# ktor
a
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
Is this what you are looking for?
e
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
thanks!! yes, these are viable alternatives but still overcomplicated I'll stick with okhttp3 websockets until this is fixed
👍 3
b
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
185 Views