Hey! :slightly_smiling_face: I'm wondering how I ...
# ktor
j
Hey! 🙂 I'm wondering how I can iterate over incoming websocket messages while using serialization as shown here?
Copy code
for (frame in incoming) {
            val data = receiveDeserialized<Data>()
            println(data)
        }
This is pretty much what I'd like to do, but the
for (frame in incoming)
part already consumes data from the queue, so I'm kinda confused how to properly iterate over it
Copy code
while (true) {
            try {
                val data = receiveDeserialized<Data>()
                println(deckInfo)
            } catch (ClosedReceiveChannelException: Exception) {
                break
            }
        }
This is what I came up as a solution, but it seems very unelegant
d
Copy code
for (frame in incoming) {
    if (frame is Frame.Text) {
        val text = frame.readText()
        // ... deserialize the text using a jackson object mapper 
    }
}
Like this?
a
There should be a method for deserializing frames without receiving them so I’ve created a feature request.
j
Thanks a lot! According to the answer in the raised issue you can deserialize Frames directly with the
deserialize
function of
WebSocketConverter
. It further says that "`WebSocketConverter` is accessible as property in
WebSocketSession
". I sadly still don't quite understand how to access
WebSocketConverter
😬
d
Copy code
for (frame in incoming) {
                    val item = converter?.deserialize(
                        charset = Charset.forName("UTF-8"),
                        typeInfo = typeInfo<SomeThing>() ,
                        content = frame)
                      // do stuff 
}
Example how you can deserialize (SomeThing in this case)
j
Super cool, thanks a lot!
d
np