Class WebSocketMessageQueue


  • public class WebSocketMessageQueue
    extends java.lang.Object
    Provides a mechanism for waiting for WebSocketMessage's to arrive from the server.

    As soon as a WebSocket is opened, the server can start sending messages to the client. It is often necessary to wait for a particular message to arrive from the server before the script can continue. This class provides an easy way to wait for a message to arrive.

    The following example demonstrates waiting for a message to arrive from the server.

    
     Url url = new Url("ws://somecompany.com/");
     WebSocket webSocket = getWebBrowser().createWebSocket(url, 1);
     webSocket.open();
      
     // Create a message queue
     WebSocketMessageQueue messageQueue = new WebSocketMessageQueue(webSocket);
      
     // Wait for the "ready" message to arrive from the server, waiting up to 30 seconds between messages.
     WebSocketMessage message = messageQueue.waitForMessageContaining("ready", 30000);
     
    • Constructor Detail

      • WebSocketMessageQueue

        public WebSocketMessageQueue​(WebSocket webSocket)
        Creates a new WebSocketMessageQueue instance to wait for messages on a particular WebSocket.
        Parameters:
        webSocket - The WebSocket that this queue will receive messages from.
    • Method Detail

      • getNextMessage

        public WebSocketMessage getNextMessage​(long timeout)
                                        throws java.lang.InterruptedException,
                                               com.facilita.exception.TimeoutException
        Get the next WebSocketMessage in the queue. If the queue is empty, wait for up to timeout milliseconds for a message to arrive.
        Parameters:
        timeout - The maximum number of milliseconds to wait for a message to arrive from the server.
        Returns:
        The next WebSocketMessage in the queue.
        Throws:
        java.lang.InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
        com.facilita.exception.TimeoutException - No message arrived from the server before the timeout period elapsed.
      • waitForMessageContaining

        public WebSocketMessage waitForMessageContaining​(java.lang.String text,
                                                         long perMessageTimeout)
                                                  throws com.facilita.exception.TimeoutException,
                                                         java.lang.InterruptedException,
                                                         java.lang.Exception
        Wait for a WebSocketMessage containing a specific piece of text to arrive from the server.
        Parameters:
        text - The text to look for in each WebSocketMessage.
        perMessageTimeout - The maximum number of milliseconds to wait for each message to arrive from the server.
        Returns:
        The first message to arrive from the server WebSocketMessage that contains text.
        Throws:
        com.facilita.exception.TimeoutException - No message arrived from the server before the perMessageTimeout period elapsed.
        java.lang.InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
        java.lang.Exception - An error occurred