Package com.facilita.fc.web
Class WebSocketMessageQueue
- java.lang.Object
-
- com.facilita.fc.web.WebSocketMessageQueue
-
public class WebSocketMessageQueue extends java.lang.ObjectProvides a mechanism for waiting forWebSocketMessage's to arrive from the server.As soon as a
WebSocketis 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 WebSocketMessagegetNextMessage(long timeout)Get the nextWebSocketMessagein the queue.WebSocketMessagewaitForMessageContaining(java.lang.String text, long perMessageTimeout)Wait for aWebSocketMessagecontaining 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 nextWebSocketMessagein the queue. If the queue is empty, wait for up totimeoutmilliseconds 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
WebSocketMessagein 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 thetimeoutperiod elapsed.
-
waitForMessageContaining
public WebSocketMessage waitForMessageContaining(java.lang.String text, long perMessageTimeout) throws com.facilita.exception.TimeoutException, java.lang.InterruptedException, java.lang.Exception
Wait for aWebSocketMessagecontaining 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
WebSocketMessagethat containstext. - Throws:
com.facilita.exception.TimeoutException- No message arrived from the server before theperMessageTimeoutperiod 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
-
-