Skip to content

Question: how to work with the WebSocketClient from the 'com.soywiz.korio.net.ws' package? #1234

Answered by soywiz
benifest asked this question in Q&A
Discussion options

You must be logged in to vote

The thing is that once connected, the program continues, and it finishes when reaching the end even if there are pending connections. If you don't have an event loop, you should wait until a condition triggers.

You could do for example:

suspend fun main() {
    val closed = Signal<Unit>()

    val ws = WebSocketClient("ws://localhost:8000/ws") {
        onOpen.add { println("onOpen") }
        onError.add { println("onError") }
        onStringMessage.add { println("onStringMessage") }
        onClose.add { println("onClose"); closed() }
    }

    closed.waitOne()
    // while (true) delay(1.seconds) // alternatively we can wait forever
}

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@benifest
Comment options

@soywiz
Comment options

@benifest
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by soywiz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants