Andi J
06/09/2022, 7:03 PMWhen handling a session, you can check a frame type, for example:
• Frame.Text is a text frame. For this frame type, you can read its content using Frame.Text.readText().
• Frame.Binary is a binary frame. For this type, you can read its content using Frame.Binary.readBytes().
• Frame.Close is a closing frame. You can call Frame.Close.readReason() to get a close reason for the current session.
Use the close function to send a close frame with the specified reason.
I need to remove my Connection instance from a set, when the channel is closed. But in this code the cleanup is never called, when the other side uses the close function:
try {
for (frame in session.incoming) {
when (frame) {
is Frame.Text -> {
// ...
}
is Frame.Close -> {
// Do cleanup
}
else -> {}
}
}
} catch (e: ClosedReceiveChannelException) {
println("onClose ${session.closeReason.await()}")
// Do cleanup
} catch (e: Throwable) {
println("onError ${session.closeReason.await()}")
// Do cleanup
}
So how do I react to closing frames correctly?Aleksei Tirman [JB]
06/09/2022, 7:44 PMcatch
block with the ClosedReceiveChannelException
or a finally
block is called when a connection is closed. Do you develop an application with a server or a client?Andi J
06/09/2022, 8:33 PMcatch
block isn't called, but the finally
block is indeed entered on the server. 🙂 I was so confused, that neither the when
branch for Frame.Close
nor the catch
did anything, so I didn't think about finally
.
I think the documentation is misleading. It states, that you can check the frame type, but closing frames don't appear when iterating over incoming
.Aleksei Tirman [JB]
06/09/2022, 9:45 PM