question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebSocketClient should allow for listening multiple times

See original GitHub issue

Currently, once the WebSocketClient produced by karate.webSocket(url, handlerDelegate); has responded to it’s listen method, subsequent calls to listen will always return the first message returned. This is not helpful, particularly when you may change the handler via setTextHandler after the first listen.

Here’s the rough scenario I’m running into.

Our WebSocket API requires authentication sent thru the first frame before we will receive messages. That authentication may take a few seconds. Currently, as soon as I send the auth frame in Karate, the rest of the suite executes. Since the authentication did not complete, we don’t receive any of the expected messages on from the socket connection. Our objective would be to send the auth frame, then wait for a response. Once we have the expected auth response, continue with the rest of our test and then listen for the next expected web socket.

Our code roughly follows this pattern:

var msgHandlerDelegate = function(msg) {
  var jsonMsg = JSON.parse(msg);
  if (jsonMessage.eventType == 'ExpectedEvent') {
    return true;
  }
  return false;
};

var authHandlerDelegate = function(msg) {
  var jsonMsg = JSON.parse(msg);
  if (jsonMsg.statusCode == 200 && jsonMsg.body == "OK") {
    return true;
  }
  return false;
};

var webSocketClient = karate.webSocket(websocketUrl, authHandlerDelegate);

var authPayload = { "message": "auth","data": { "auth_token": auth_token } };
webSocketClient.send(JSON.stringify(authPayload));
var response = webSocketClient.listen(20000);

expect response != null;

webSocketClient.setTextHandler(karate.toJava(msgHandlerDelegate));

// Perform remainder of test

var webSocketMessage = webSocketClient.listen(20000);
expect webSocketMessage == { eventType: 'ExpectedEvent' }

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
jon-armencommented, Aug 10, 2022

@ptrthomas , Apologies for not getting back sooner, No worries about a breaking change on the implementation. I’ll try to test it out in the next few days.

1reaction
jon-armencommented, May 10, 2022

@ptrthomas , thank you for your responsiveness getting these changes incorporated. much appreciated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Replying multiple times over web-socket without spring ...
I need to send multiple responses to a request over the web socket that sent the request message, the first one quickly, and...
Read more >
Writing WebSocket client applications - Web APIs | MDN
WebSocket client applications use the WebSocket API to communicate with WebSocket servers using the WebSocket protocol.
Read more >
WebSocket: Simultaneous Bi-Directional Client-Server ...
WebSocket allows a single TCP socket connection to be hijacked so that ... Will the client initiate the WebSocket handshake multiple times?
Read more >
Building a real-time WebSocket client - AWS AppSync
If there is a network error, the client should do a jittered exponential backoff. For more information, see Exponential backoff and jitter on...
Read more >
WebSockets for fun and profit - Stack Overflow Blog
To fully appreciate the power of WebSocket, let's take a step back and look at a few common ways that computers can fetch...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found