For our integration tests we have a scenario where we want to listen to a set amount of messages predefined by the environment we use. I've seen that it's possible to listen to multiple messages by opening up a new connection but that doesn't allow for much flexibility.
Have you read the docs, because as far as I know, if you define a "handler" function, you can use the same connection for multiple messages and choose when you want to stop: https://github.com/intuit/karate#websocket
Also see: https://stackoverflow.com/a/67870765/143475
But if you have a very specific need or custom logic, maybe the best thing is is to write a small piece of Java "glue" code and you will get all the flexibility that you want. You may be able to re-use Karate's Java API such as com.intuit.karate.http.WebSocketClient
- but this is not documented, and potentially an area where you can research / contribute code.
Here is a good example: https://twitter.com/KarateDSL/status/1417023536082812935 of the flexibility that the Java-interop approach provides.