Animesh Sahu
Animesh Sahu

Reputation: 8106

How to send pings on Ktor websockets

I tried to search in api docs as well as examples, but there weren't any example demonstrating how to send pings and receive pings. The only example was of how to connect to websocket and to send a text here. I also saw chat sample here of server side and i carefully followed that as well (i.e. set ping interval in server side configuration of WebSocket installation).

I start listening both side for pongs but none of the side was receiving any ping messages.

And there is no option to configure the client side for pings as you can see here.

I'm so much confused about how to send pings.

This is my server side:

embeddedServer(
    CIO,
    80
) {
    install(io.ktor.websocket.WebSockets) {
        pingPeriod = Duration.ofSeconds(20)
    }

    routing {
        webSocket("/ws") {
            for (frame in incoming) {
                when (frame) {
                    is Frame.Pong -> {
                        println("ping's response recieved")
                    }

                    is Frame.Ping -> {
                        // just temporary block
                        println("ping recieved")
                    }

                    is Frame.Text -> {
                        println(frame.readText())
                    }
                }
            }
        }
    }
}.apply { start() }

This is my client side:

val client = HttpClient(CIO) {
    install(WebSockets)
}

client.ws(
    method = HttpMethod.Get,
    host = "127.0.0.1",
    port = 80,
    path = "/ws"
) {

    send(Frame.Text("Hello World!"))

    for (frame in incoming) {
        when (frame) {
            is Frame.Pong -> {
                println("ping's response received")
            }

            is Frame.Ping -> {
                // just temporary block
                println("ping recieved from server")
            }

            is Frame.Text -> {
                println(frame.readText())
            }
        }
    }
}

Result:

Hello World!

i.e websocket is connected, text are able to transferred, but unfortunately can't use ping/pong feature.

I also found some functions for this here pinger and ponger but now it says its part of api and gets automatically start with initiation of WebsocketSession and i also tried to put pinger in client side but that didn't send ping to server whatsoever.

Result of above code is simply Hello world gets printed in server console as sent from client side but no ping received messages.

Upvotes: 2

Views: 3452

Answers (1)

Eric
Eric

Reputation: 5402

I was having trouble getting OkHttp to use ping/pong so I filed this issue https://github.com/ktorio/ktor/issues/1803 and one of the developers replied "well the only thing I can recommend you is to try CIO out. Is does support manual Ping/Pong processing using RawWebSockets."

Haven't tried it myself but you should check out https://github.com/ktorio/ktor/blob/master/ktor-features/ktor-websockets/jvm/test/io/ktor/tests/websocket/RawWebSocketTest.kt

Upvotes: 2

Related Questions