diff --git a/karate-core/src/main/java/com/intuit/karate/http/WebSocketClient.java b/karate-core/src/main/java/com/intuit/karate/http/WebSocketClient.java index 66b17713c..e7fe37f1d 100644 --- a/karate-core/src/main/java/com/intuit/karate/http/WebSocketClient.java +++ b/karate-core/src/main/java/com/intuit/karate/http/WebSocketClient.java @@ -210,7 +210,9 @@ public void signal(Object result) { public synchronized Object listen(long timeout) { try { logger.trace("entered listen wait state"); - return SIGNAL.get(timeout, TimeUnit.MILLISECONDS); + Object result = SIGNAL.get(timeout, TimeUnit.MILLISECONDS); + SIGNAL = new CompletableFuture(); + return result; } catch (Exception e) { logger.error("listen timed out: {}", e + ""); return null; diff --git a/karate-demo/src/test/java/demo/websocket/websocket.feature b/karate-demo/src/test/java/demo/websocket/websocket.feature index a2cb4a9fc..71087a5ae 100644 --- a/karate-demo/src/test/java/demo/websocket/websocket.feature +++ b/karate-demo/src/test/java/demo/websocket/websocket.feature @@ -24,3 +24,25 @@ Scenario: using the websocket instance to send as well as receive messages * socket.send('Billie') * def result = socket.listen(5000) * match result == 'hello Billie !' + +Scenario: listen for multiple websocket messages + * def handler = function(msg){ return msg.startsWith('hello') } + * def socket = karate.webSocket(demoBaseUrl + '/websocket', handler) + * socket.send('Billie') + * def result = socket.listen(5000) + * match result == 'hello Billie !' + * socket.send('Bob') + * def result = socket.listen(5000) + * match result == 'hello Bob !' + +Scenario: change the websocket handler for messages + * def handler = function(msg){ return msg.contains('Billie') } + * def socket = karate.webSocket(demoBaseUrl + '/websocket', handler) + * socket.send('Billie') + * def result = socket.listen(5000) + * match result == 'hello Billie !' + * def handler = function(msg){ return msg.contains('Bob') } + * socket.setTextHandler(karate.toJava(handler)) + * socket.send('Bob') + * def result = socket.listen(5000) + * match result == 'hello Bob !' \ No newline at end of file