use Cro::WebSocket::Client; say "Connecting..."; my $conn = try await Cro::WebSocket::Client.connect( 'ws://localhost:8080' ); say "WebSocket handshake failed!" and exit unless $conn; say "Connected"; react { # It is guarantueed, only one whenever block in this react block can be running at any given time # There are no race conditions, for $conn whenever $conn.messages -> $message { say "Incoming: ", await $message.body; } # Currently this part only works for me since I patched in the closing - supply. # But I am confident my pull request will be accecpted in some form # Maybe not with that exact name whenever $conn.closing -> $reason { say "Closing: $reason"; done; } # Without the above, one would have to check here for # $conn.closed != True before calling `send` whenever Supply.interval(5) -> $tick { my $command = ("RUN", "STOP").pick; say "Sending: $command"; $conn.send( $command ); CATCH { say "Error sending <$command>: {.message}" } } };