I want to close it explicitly when it is no longer...
# ktor
n
I want to close it explicitly when it is no longer needed
m
Returning from suspend function closes WS, AFAIK.
n
yeah, but that's weird... why would I want to do this?
websockets are not AJAX.. they are meant to be persistent
maybe I just don't understand how to use the mechanism properly
I would be grateful for a hint on how to manage this situation properly
m
Suspend function invocation may long several hours/days, it's OK. You can use
delay()
and many other non-blocking (suspending) waiting mechanisms.
n
@miha-x64 could you please elaborate on what would be the best way to suspend the execution of the websocket invocation while I save the session and use it from elsewhere?
I can imagine creating some kind of a lock, like a boolean value to indicate from elsewhere that the websocket can be released, and in wait for it inside the websocket routine...
but I'm not sure what would be the best choice for the waiting mechanism here...
m
You can use low-level coroutines API to manually suspend/resume function. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/suspend-coroutine.html
I highly recommend you watching Roman Elizarov's talk about coroutines:

https://www.youtube.com/watch?v=YrrUCSi72E8â–¾

Channel for coroutines-related Q&A and discussions: #coroutines
n
@miha-x64 Thank you very much! 🙂