Class WebSocket
- java.lang.Object
-
- com.facilita.fc.web.WebSocket
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class WebSocket extends java.lang.Object implements java.lang.AutoCloseableRepresents a WebSocketA WebSocket object is created by calling
WebBrowser.createWebSocket(Url, int). The WebSocket can then be opened by calling theopen()method. Once the WebSocket has been opened, then messages can be sent to the server using thesendMessage(String)method. Messages arriving from the server can be processed by registering a callback by calling theregisterReceivedMessageCallback(WebSocketReceivedMessageCallback)method.The following example demonstrates creating and opening a WebSocket:
Url url = new Url("ws://echo.websocket.org/"); WebSocket webSocket = getWebBrowser().createWebSocket(url, 1); webSocket.open();
-
-
Constructor Summary
Constructors Constructor Description WebSocket(long cPtr, boolean cMemoryOwn)For internal use only.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearHeaders()Clears the list of HTTP headers for this WebSocket.voidclose()(non-Javadoc)voidclose(java.lang.String reason)Closes the WebSocket.booleancontainsHeader(java.lang.String header)Determines whether this WebSocket contains a particular HTTP header.voiddelete()java.lang.StringgetContentType()Gets the value of the"Content-Type"HTTP header.static longgetCPtr(WebSocket obj)For internal use only.java.util.List<java.lang.String>getHeaderKeys()Gets a list of names of all the HTTP request headers set in this Request.java.lang.StringgetHeaderValue(java.lang.String header)Gets the value of the specified HTTP request header.intgetID()Gets the unique ID that has been assigned to this WebSocket.java.lang.StringgetReferer()Gets the value of the"Referer"HTTP header.UrlgetUrl()Gets theUrlthat this WebSocket is targeting.java.lang.StringgetUserAgent()Gets the value of the"User-Agent"HTTP header.WebBrowserVirtualUsergetVU()Gets theVirtualUserinstance that this WebSocket is associated with.booleanhasReferer()Gets a value that indicates whether the"Referer"HTTP header has been set for this Request.voidopen()Opens the WebSocket.voidregisterClosedCallback(WebSocketClosedCallback callback)Register a callback to be invoked when the WebSocket is closed.voidregisterErrorCallback(WebSocketErrorCallback callback)Register a callback to be invoked when an error occurs on the WebSocket.voidregisterReceivedMessageCallback(WebSocketReceivedMessageCallback callback)Register a callback to be invoked when a message is received from the server.voidremoveHeader(java.lang.String header)Removes an HTTP header from this WebSocket.voidsendMessage(byte[] data)Send a binary message to the server.voidsendMessage(byte[] data, boolean isBinary)Send a message to the server.voidsendMessage(WebSocketMessage message)Send a message to the server.voidsendMessage(java.lang.String text)Send a text message to the server.voidsendMessageFromFile(java.lang.String path, boolean isBinary)Send a message to the server, by loading the message data from a file.voidsendMessageFromFile(java.lang.String path, boolean isBinary, int chunkSize)Send a message to the server, by loading the message data from a file.voidsetContentType(java.lang.String contentType)Sets the value of the"Content-Type"HTTP header.voidsetHeader(java.lang.String header)Sets an HTTP header to use in the opening handshake request for this WebSocket, using the contents of the specified string.voidsetHeader(java.lang.String header, java.lang.String value)Sets an HTTP header to use in the opening handshake request for this WebSocket, as a name/value pair.voidsetReferer(Url url)Sets the value of the"Referer"HTTP header.voidsetReferer(java.lang.String referer)Sets the value of the"Referer"HTTP header.voidsetUrl(Url url)Sets theUrlthat this WebSocket is targeting.voidsetUserAgent(java.lang.String userAgent)Sets the value of the"User-Agent"HTTP header.voidsetVU(WebBrowserVirtualUser vu)Sets theVirtualUserinstance that this WebSocket is associated with.voidunsetReferer()Removes the"Referer"HTTP header.
-
-
-
Method Detail
-
getCPtr
public static long getCPtr(WebSocket obj)
For internal use only. Of no interest to the user.- Parameters:
obj- A reference to an object of this class.- Returns:
- A long containing the address of the CPP wrapped object.
-
delete
public void delete()
-
close
public void close() throws java.lang.Exception(non-Javadoc)- Specified by:
closein interfacejava.lang.AutoCloseable- Throws:
java.lang.Exception- See Also:
AutoCloseable.close()
-
setVU
public void setVU(WebBrowserVirtualUser vu)
Sets theVirtualUserinstance that this WebSocket is associated with.- Parameters:
vu- TheVirtualUserinstance that this WebSocket is associated with.
-
getVU
public WebBrowserVirtualUser getVU()
Gets theVirtualUserinstance that this WebSocket is associated with.- Returns:
- The
VirtualUserinstance that this WebSocket is associated with.
-
getUrl
public Url getUrl()
Gets theUrlthat this WebSocket is targeting.- Returns:
- The target
Url - See Also:
setUrl(Url),WebBrowser.createWebSocket(Url, int)
-
setUrl
public void setUrl(Url url)
Sets theUrlthat this WebSocket is targeting.- Parameters:
url- The targetUrl- See Also:
getUrl(),WebBrowser.createWebSocket(Url, int)
-
sendMessage
public void sendMessage(java.lang.String text)
Send a text message to the server.The WebSocket must be opened, by calling the
open()method, before any messages can be sent to the server. The text will be encoded as UTF-8 before being sent to the server.- Parameters:
text- The message to send- See Also:
sendMessage(byte[], boolean),open()
-
sendMessage
public void sendMessage(byte[] data, boolean isBinary)Send a message to the server.The WebSocket must be opened, by calling the
open()method, before any messages can be sent to the server.- Parameters:
data- The data to send to the serverisBinary- If set totrue, then the message will be flagged as binary data. Otherwise, it will be flagged as text.- See Also:
sendMessage(String),open()
-
sendMessage
public void sendMessage(byte[] data)
Send a binary message to the server.The WebSocket must be opened, by calling the
open()method, before any messages can be sent to the server.- Parameters:
data- The data to send to the server- See Also:
sendMessage(String),open()
-
registerReceivedMessageCallback
public void registerReceivedMessageCallback(WebSocketReceivedMessageCallback callback)
Register a callback to be invoked when a message is received from the server.Once the WebSocket has been opened, the server may send messages at any time. A callback registered with this method is called whenever a message arrives from the server. Note that the method will be called on a different thread to the thread that opened the WebSocket.
- Parameters:
callback- The instance ofWebSocketReceivedMessageCallbackthat will be called when messages arrive from the server.
-
registerClosedCallback
public void registerClosedCallback(WebSocketClosedCallback callback)
Register a callback to be invoked when the WebSocket is closed.Note that the WebSocket close may be triggered by either the client or the server.
- Parameters:
callback- The instance ofWebSocketClosedCallbackthat will be called when the WebSocket is closed.- See Also:
close(String)
-
registerErrorCallback
public void registerErrorCallback(WebSocketErrorCallback callback)
Register a callback to be invoked when an error occurs on the WebSocket.An error could occur during sending, receiving, or closing of the WebSocket.
- Parameters:
callback- The instance ofWebSocketErrorCallbackthat will be called when an error occurs.
-
open
public void open()
Opens the WebSocket.Opens the WebSocket by performing the opening handshake. The opening handshake is an HTTP Upgrade request. If the handshake is successful, then the server will respond with a 101 Switching Protocols HTTP response.
- See Also:
close(String)
-
close
public void close(java.lang.String reason)
Closes the WebSocket.- Parameters:
reason- The reason for closing the websocket to send to the server- See Also:
open()
-
sendMessage
public void sendMessage(WebSocketMessage message)
Send a message to the server.The WebSocket must be opened, by calling the
open()method, before any messages can be sent to the server. You can create aWebSocketMessageobject by callingWebBrowser.createWebSocketMessage(String).- Parameters:
message- The message to send to the server- See Also:
sendMessage(String),open(),WebBrowser.createWebSocketMessage(String)
-
sendMessageFromFile
public void sendMessageFromFile(java.lang.String path, boolean isBinary, int chunkSize)Send a message to the server, by loading the message data from a file.The WebSocket must be opened, by calling the
open()method, before any messages can be sent to the server.If you are using this method in your script, make sure that the file containing the data is in the
data/Filesdirectory within your project.- Parameters:
path- The path of the filename, relative to the Virtual User's data directory.isBinary- If set totrue, then the message will be flagged as binary data. Otherwise, it will be flagged as text.chunkSize- The number of bytes to send at a time.- See Also:
sendMessage(String),open()
-
sendMessageFromFile
public void sendMessageFromFile(java.lang.String path, boolean isBinary)Send a message to the server, by loading the message data from a file.The WebSocket must be opened, by calling the
open()method, before any messages can be sent to the server.If you are using this method in your script, make sure that the file containing the data is in the
data/Filesdirectory within your project. 4096 bytes will be sent to the server at a time. If you want to use a different chunk size, then use thesendMessageFromFile(String, boolean, int)overload.- Parameters:
path- The path of the filename, relative to the Virtual User's data directory.isBinary- If set totrue, then the message will be flagged as binary data. Otherwise, it will be flagged as text.- See Also:
sendMessage(String),open()
-
getID
public int getID()
Gets the unique ID that has been assigned to this WebSocket.- Returns:
- This ID number is created by the
WebBrowser. It is the value that is displayed in the web log viewer.
-
setHeader
public void setHeader(java.lang.String header, java.lang.String value)Sets an HTTP header to use in the opening handshake request for this WebSocket, as a name/value pair.The header will be sent to the web server when this WebSocket is opened.
The following example demonstrates setting the
"X-Requested-With"header on the request.webSocket.setHeader("X-Requested-With", "XMLHttpRequest");- Parameters:
header- The header to set.value- The value to assign to the header.- See Also:
removeHeader(String),clearHeaders()
-
setHeader
public void setHeader(java.lang.String header)
Sets an HTTP header to use in the opening handshake request for this WebSocket, using the contents of the specified string.The header will be sent to the web server when this WebSocket is opened. The following example demonstrates setting the
"X-Requested-With"header on the request.webSocket.setHeader("X-Requested-With: XMLHttpRequest");- Parameters:
header- The header to set, including the value, which should be separated from the name with a colon.- See Also:
removeHeader(String),clearHeaders()
-
removeHeader
public void removeHeader(java.lang.String header)
Removes an HTTP header from this WebSocket.- Parameters:
header- The header to remove.- See Also:
setHeader(String, String),clearHeaders()
-
clearHeaders
public void clearHeaders()
Clears the list of HTTP headers for this WebSocket.- See Also:
setHeader(String, String),removeHeader(String)
-
setReferer
public void setReferer(Url url)
Sets the value of the"Referer"HTTP header.- Parameters:
url- The value to set.- See Also:
getReferer()
-
setUserAgent
public void setUserAgent(java.lang.String userAgent)
Sets the value of the"User-Agent"HTTP header.- Parameters:
userAgent- The value of the"User-Agent"HTTP header.- See Also:
getUserAgent()
-
setReferer
public void setReferer(java.lang.String referer)
Sets the value of the"Referer"HTTP header.- Parameters:
referer- The value to set.- See Also:
getReferer()
-
setContentType
public void setContentType(java.lang.String contentType)
Sets the value of the"Content-Type"HTTP header.- Parameters:
contentType- The value of the"Content-Type"HTTP header.- See Also:
getContentType()
-
unsetReferer
public void unsetReferer()
Removes the"Referer"HTTP header.
-
containsHeader
public boolean containsHeader(java.lang.String header)
Determines whether this WebSocket contains a particular HTTP header. The following example demonstrates getting the"Content-Type"header of a WebSocket request.if (webSocket.containsHeader("Content-Type")) { writeMessage(String.format("Content-Type=%s", webSocket.getHeaderValue("Content-Type"))); }- Parameters:
header- The HTTP header to check for.- Returns:
trueif this WebSocket contains the specified header; otherwise,false.
-
getHeaderValue
public java.lang.String getHeaderValue(java.lang.String header)
Gets the value of the specified HTTP request header.- Parameters:
header- The header to get the value of.- Returns:
- The value of the header.
- See Also:
setHeader(String, String),removeHeader(String)
-
getUserAgent
public java.lang.String getUserAgent()
Gets the value of the"User-Agent"HTTP header.- Returns:
- the value of the
"User-Agent"HTTP header.
-
getReferer
public java.lang.String getReferer()
Gets the value of the"Referer"HTTP header.- Returns:
- the value of the
"Referer"HTTP header.
-
hasReferer
public boolean hasReferer()
Gets a value that indicates whether the"Referer"HTTP header has been set for this Request.- Returns:
trueif the"Referer"header has been set; otherwise,false.
-
getContentType
public java.lang.String getContentType()
Gets the value of the"Content-Type"HTTP header.- Returns:
- the value of the
"Content-Type"HTTP header.
-
getHeaderKeys
public java.util.List<java.lang.String> getHeaderKeys() throws com.facilita.exception.InternalErrorGets a list of names of all the HTTP request headers set in this Request.- Returns:
- A list of names of all the HTTP request headers set in this Request.
- Throws:
com.facilita.exception.InternalError- An error occurred
-
-