Hello Teams :smile: I am doing my server with kto...
# ktor
n
Hello Teams 😄 I am doing my server with ktor and I used webSocket to do a chat, I hope my question will not be dumb because I am iOS developer at firt 😄 I have the following code to set up my WebSocket with ktor. My problem is the following: if I turn off the internet on the mobile side (iOS) after setup a connection with the socket for about 3 minutes, my socket is no longer active, probably because the ping/pong fails on front side from iOS side . However, the finally block on server side is often called a long time ago, so the webSocket is close after 20 minutes. How can I prevent this memory leak? and to be sure that after 3minute offline from the client, the webSocket will be remove from my server please. I Have setup the ping pong like this
Copy code
fun Application.configureWebSocket(){
    install(WebSockets) {
        pingPeriod = 15.seconds
        timeout = 15.seconds
        maxFrameSize = kotlin.Long.MAX_VALUE
        masking = false

    }
}
Copy code
routing {
    webSocket("ws") {
        val token = call.request.queryParameters["token"]
        if (token == null) {
            close(CloseReason(CloseReason.Codes.VIOLATED_POLICY, "No Token"))
            return@webSocket
        }

        val decodedJWT = try { JwtFactory.buildverifier().verify(token) }
        catch (e: Exception) {
            close(CloseReason(CloseReason.Codes.VIOLATED_POLICY, "Invalid Token: ${e.message}"))
            return@webSocket
        }

        val userId: UUID = try { UUID.fromString(decodedJWT.getClaim(JwtClaimConstant.claimUserId).asString())  }
        catch (e: Exception) {
            close(CloseReason(CloseReason.Codes.VIOLATED_POLICY, "Invalid Token: ${e.message}"))
            return@webSocket
        }

        val sessionId = decodedJWT.id?.let {
            runCatching { UUID.fromString(it) }.getOrNull()
        } ?: run {
            close(CloseReason(CloseReason.Codes.VIOLATED_POLICY, "Invalid or missing sessionId (jti)"))
            return@webSocket
        }
        <http://logger.info|logger.info>("$userId is connected")

        try {
            println("$userId start")
            incoming.consumeEach {
                when (it) {

                    is Frame.Text -> {
                        val text = it.readText()
                        println("tototot $userId Received: $text")
                    }
                    is Frame.Close -> {
                        println("tototot $userId WebSocket closed by server with reason: ${it.readReason()}")
                    }
                    is Frame.Ping -> {
                        println("tototot $userId ping: $it")
                    }
                    is Frame.Pong -> {
                        println("tototot $userId pong: $it")
                    }  else -> {
                        println("tototot $userId else: $it")
                    }
                }


            }
        } catch (e: Exception) {
            println("$userId error $e")
         } finally {
            println("$userId finally remove")
        }

        println("$userId end")
    }
}
and I found something weird If I open a websocket on my iOS phone, turnoff the internet (and close my phone) I keep logging this on KTOR: io.ktor.websocket.WebSocket - WebSocket Pinger: received valid pong frame Frame PONG For me if I close my phone the webscket should try a ping and should not receive a pong and close, For me the goal of ping pong is to avoid all this. I am not sure if I have doing everything good 🙂 I am hosting my server on Render Thank you in advance for your time
a
n
hello @Aleksei Tirman [JB] thank for your answer, I have the issue only on the server 😢 I have no issue with the client, if I turn off the wifi the websocket connection from the client will close but not the websocket connection from server it will be stay alive 20 minutes left
but to answer to your answer it's look like the pong is received on the server because I have this log TRACE io.ktor.websocket.WebSocket - WebSocket Pinger: received valid pong frame Frame PONG (fin=true, buffer len = 76)
I don't knwo were from the pong because my iPhone was turn off for this test
a
Can you please check the timestamps of those messages?
n
I have this message each 15 secondes during 20 minutes
a
Even if you turn the network connection off on your client?
n
yes
and even my phone close
a
Can you please try to reproduce the problem with another client?
n
I have only try with iphone
I don't have other client
hello 😄
but for me the issue don't come from client
because the phone it's off
a
I don't quite understand what sends the PING frames if the phone's network is off. Can you please check that with a traffic analyzing tool like Wireshark?
n
ktor sever host on Render send a ping to turn off iphone and In the log It say that received a pong but if you try to connect with an iOS on webSocket (or android phone) and close your phone the websock will close on your side 🙂 ? I don't know how to use Wireshark 😢