Package com.facilita.fc.web
Class WebSocketMessageQueue
- java.lang.Object
-
- com.facilita.fc.web.WebSocketMessageQueue
-
public class WebSocketMessageQueue extends java.lang.Object
Provides a mechanism for waiting forWebSocketMessage
'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 Summary
Constructors Constructor Description WebSocketMessageQueue(WebSocket webSocket)
Creates a new WebSocketMessageQueue instance to wait for messages on a particularWebSocket
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description WebSocketMessage
getNextMessage(long timeout)
Get the nextWebSocketMessage
in the queue.WebSocketMessage
waitForMessageContaining(java.lang.String text, long perMessageTimeout)
Wait for aWebSocketMessage
containing a specific piece of text to arrive from the server.
-
-
-
Method Detail
-
getNextMessage
public WebSocketMessage getNextMessage(long timeout) throws java.lang.InterruptedException, com.facilita.exception.TimeoutException
Get the nextWebSocketMessage
in the queue. If the queue is empty, wait for up totimeout
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 thetimeout
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 aWebSocketMessage
containing a specific piece of text to arrive from the server.- Parameters:
text
- The text to look for in eachWebSocketMessage
.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 containstext
. - Throws:
com.facilita.exception.TimeoutException
- No message arrived from the server before theperMessageTimeout
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
-
-