Using OPENRNDR as a websocket client

Ok I think this is much cleaner, with the WebSocket listening outside the draw loop.

There’s no need to keep creating Coroutines inside extends { }. Just create one with an infinite loop that waits for new messages to arrive. It seems to automatically sleep when there’s nothing received.

fun main() = application {
    val client = HttpClient(CIO) {
        install(WebSockets) {
            pingInterval = 10_000
        }
    }

    program {
        val session = client.webSocketSession(HttpMethod.Get, "localhost", 8080, "/")
        val radius = AtomicInteger(140)

        GlobalScope.launch {
            while (true) {
                val msg = session.incoming.receive() as Frame.Text
                radius.set(Random.int(50, 200))
                println(msg.readText())
            }
        }

        extend {
            drawer.fill = ColorRGBa.PINK
            drawer.circle(
                Polar(seconds * 50, 200.0).cartesian + drawer.bounds.center, radius.toDouble()
            )
        }
    }
}
1 Like