Skip to main content

WebSocket Rule

The WebSocket rule is one of the rules you can apply when you generate a web virtual user (VU) script in Eggplant Performance Studio using the Generation Rules wizard. You can use the WebSocket rule to change how WebSocket script code is generated for WebSocket requests in the recording.

Diagram showing how the WebSocket rule works for script generation in Eggplant Performance Studio

WebSocket traffic is different from HTTP/HTTPS traffic in that the connection is bidirectional, meaning that traffic can head in both directions at the same time. This protocol is also known as a full-duplex communication channel.

With the Eggplant Performance Studio Web Proxy Recorder, both outgoing (client to server) and incoming (server to client) messages get recorded. Because of the asynchronous nature of WebSocket traffic, you might want to add a WebSocket rule so that your script waits for a message from the server before proceeding to another action, for example.

Example Script Code Changes (Java)

Url url3 = new Url(protocol1, eppdev, "/StockTicker/signalr/connect");

QueryData queryData3 = new QueryData();
queryData3.add("transport", "webSockets");
queryData3.add("clientProtocol", "1.5");
queryData3.add("connectionToken",
"XasuYYF6vjGX4mb+labzvrTFiL2BSYolwtDxan8h5VzHIxXwjhxmWKQUUdb"+
"zi78QNyfwObmNNzfFZgfLETyt7LT4unvvcjHPQ/kS"+
"Jtt1b+qAO24c00tQwpHZ4xkeqXPk");
queryData3.add("connectionData", "[{\"name\":\"stockticker\"}]");
queryData3.add("tid", "3");
url3 = url3.withQuery(queryData3);

WebSocket webSocket3 = getWebBrowser().createWebSocket(url3, 3);
webSocket3.setHeader("Origin", "http://eppdev.testplant.com");

webSocket3.registerReceivedMessageCallback((ws, message) -> webSocket3_onReceivedMessage(ws, message));
webSocket3.registerErrorCallback((ws, errorMessage) -> webSocket3_onError(ws, errorMessage));
webSocket3.registerClosedCallback((ws, closedByClient, reason)-> webSocket3_onClosed(ws, closedByClient, reason));

// #region EPP_BEFORE_WEB_SOCKET_OPENED for WebSocket 3

// Code added here will be preserved during script regeneration

// #endregion EPP_BEFORE_WEB_SOCKET_OPENED for WebSocket 3

webSocket3.open();

// #region EPP_AFTER_WEB_SOCKET_OPENED for WebSocket 3

// Code added here will be preserved during script regeneration

// #endregion EPP_AFTER_WEB_SOCKET_OPENED for WebSocket 3

set("webSocket3", webSocket3);

// webSocket3 message 1
// Received text message: {\"C\":\"d-2502C491-K,0|L,0|M,1\",\"S\":1,\"M\":[]}

Generation Rules Wizard Pages

Choose what code to generate for WebSocket messages

Choose options to apply to WebSocket code in the Generation Rules Wizard in Eggplant Performance Studio

Use the options on this page to customize WebSocket code in your scripts.

In the Outgoing messages section, choose one of the following options, which gets applied to all outgoing WebSocket messages:

  • Generate code to send messages to the server: (Default) Choose this option if you want the generator to plant code to send all the outgoing messages in the recording to the server.
  • Generate comments showing the data sent to the server: Choose this option if you want the generator to plant comments in the script showing what messages were sent to the server.
  • Generate nothing: Choose this option if you do not want to generate any code for sending messages to the server. For example, you might want to add code to the event handlers instead.

In the Incoming messages section, choose one of the following options for handling messages received from the server:

  • Generate comments showing the data received from the server: (Default) Select this option if you want the generator to plant comments in the script showing when messages were received from the server.
  • Generate code to wait for the messages to arrive from the server: Choose this option if you want the generator to plant code that forces the script to wait for each recorded response to arrive from the server. If you select this option, use the Timeout field to set the maximum wait time for each message to arrive from the server.
  • Generate nothing: Use this option if you do not want the generator to plant any code to handle messages arriving from the server.

The Event handlers section has one option:

  • Generate event handlers for each WebSocket: If you select this option, the generator creates event handlers for each WebSocket in the recording. Event handlers are created to handle receiving messages, closing the socket, and errors.

Click Next after making your choices.

After the Choose what code to generate for WebSocket messages page, the common pages Create Request Filter and Rule name and summary appear.