public class WebSocketClient
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
CONNECT_STEP_CLOSED
WebSocket internal state: Previously opened websocket now closed.
|
static int |
CONNECT_STEP_HTTP_RECEIVE_UPGRADE_RESPONSE
WebSocket internal state: Receiving the HTTP upgrade response from server.
|
static int |
CONNECT_STEP_HTTP_SEND_UPGRADE_REQUEST
WebSocket internal state: Sending the HTTP upgrade request to server.
|
static int |
CONNECT_STEP_HTTP_VERIFY_UPGRADE_RESPONSE
WebSocket internal state: Verifying the HTTP upgrade server response.
|
static int |
CONNECT_STEP_NO_STEP
WebSocket internal state: The connect() method was not called.
|
static int |
CONNECT_STEP_OPEN
WebSocket internal state: WebSocket open to server.
|
static int |
CONNECT_STEP_PREPARE
WebSocket internal state: Preparing the connection to server.
|
static int |
CONNECT_STEP_SSL_HANDSHAKE
WebSocket internal state: Performing the SSL handshake with server.
|
static int |
CONNECT_STEP_TCP_CONNECT
WebSocket internal state: Establishing the TCP/IP connection to server
|
static int |
DEFAULT_CONNECT_TIMEOUT_MILLIS
The default value for the socket connect timeout in milliseconds.
|
static long |
DEFAULT_FRAME_SEND_TIMEOUT_MILLIS
The default value for the frame send timeout in milliseconds.
|
static java.lang.String |
DEFAULT_TLS_PROTOCOL_VERSION
The default TLS protocol version for encrypted wss connections.
|
static java.lang.String |
PROTOCOL_WS
The unencrypted 'ws' protocol.
|
static java.lang.String |
PROTOCOL_WSS
The encrypted 'wss' protocol.
|
Constructor and Description |
---|
WebSocketClient(java.lang.String serverHost,
int serverPort,
java.lang.String urlPath,
WebSocketClientListenerInterface clientListener)
Create a new, unconnected WebSocket client.
|
Modifier and Type | Method and Description |
---|---|
void |
addHttpBasicAuthentication(java.lang.String username,
java.lang.String password)
Add HTTP Basic Authentication credentials to the WebSocket upgrade request.
|
void |
addHttpCookie(java.lang.String cookieName,
java.lang.String cookieValue)
Add a HTTP cookie to the WebSocket upgrade request.
|
void |
addHttpHeaderField(java.lang.String httpHeaderField)
Add an additional HTTP header field to the WebSocket upgrade request.
|
WebSocketClientContext |
connect()
Connect the WebSocket to the server.
|
static java.lang.String |
currentConnectStepToString(int connectStep)
Get the text representation of the current internal state of the WebSocket connection.
|
void |
disconnect(long serverConfirmCloseMillis)
Disconnect the WebSocket from the server.
|
int |
getConnectTimeoutMillis()
Get the value for the socket connect timeout.
|
int |
getCurrentConnectStep()
Get the current internal state of the WebSocket connection - which provides additional information if establishing a WebSocket connection to the server fails.
|
long |
getFrameSendTimeoutMillis()
Get the value for the frame send timeout.
|
long |
getHttpUpgradeRequestTime()
Get the HTTP upgrade request send time, inclusive flushing the HTTP request at network level.
|
long |
getHttpUpgradeResponseTime()
Get the HTTP upgrade response receive time, started after the HTTP upgrade request was send and flushed, until the complete HTTP response header ws received.
|
long |
getNumberOfPendingFramesToSend()
Get the number of currently pending frames to send (performance data).
|
java.lang.String |
getProtocol()
Get the protocol (ws or wss).
|
java.util.ArrayList<java.lang.String> |
getRequestHeader()
Get the list of the HTTP upgrade request header fields.
|
java.util.ArrayList<java.lang.String> |
getResponseHeader()
Get the list of the HTTP upgrade response header fields.
|
long |
getSslHandshakeTime()
Get the SSL handshake time to the server (after the TCP/IP connection was established).
|
long |
getTcpIpConnectTime()
Get the TCP/IP connect time for opening the network connection to the server.
|
javax.net.ssl.KeyManager[] |
getTLSClientAuthenticationKeyManagers()
Get the TLS client authentication credential (derived from a client certificate).
|
java.lang.String |
getTLSProtocolVersion()
Get the TLS protocol version (which is used for encrypted wss connections only).
|
long |
getTotalBytesReceived()
Get the total number of bytes which has received till now - overall frames (performance data).
|
long |
getTotalBytesSent()
Get the total number of bytes which has sent till now - overall frames (performance data).
|
long |
getTotalFramesReceived()
Get the total number of frames which has received till now (performance data).
|
long |
getTotalFramesSent()
Get the total number of frames which has sent till now (performance data).
|
long |
getTotalPayloadReceived()
Get the total sum of payload in bytes which has received till now - overall frames (performance data).
|
long |
getTotalPayloadSent()
Get the total sum of payload in bytes which has sent till now - overall frames (performance data).
|
long |
getWebSocketOpenTime()
Get the total time used to open the WebSocket to the server.
|
void |
printDebug(java.lang.String message)
Print debug information to stdout.
|
void |
setConnectTimeoutMillis(int connectTimeoutMillis)
Override the default value for the socket connect timeout.
|
void |
setDebugToStdout()
Enable that debug information is written to stdout.
|
void |
setFrameSendTimeoutMillis(long frameSendTimeoutMillis)
Override the default value for the frame send timeout.
|
void |
setHttpOrigin(java.lang.String httpOrigin)
Set the value of the HTTP origin header field.
|
void |
setProtocol(java.lang.String protocol)
Set the protocol (ws or wss).
|
void |
setTLSClientAuthenticationKeyManagers(javax.net.ssl.KeyManager[] tlsClientAuthenticationKeyManagers)
Set a TLS client authentication credential (derived from a client certificate).
|
void |
setTLSProtocolVersion(java.lang.String tlsProtocolVersion)
Override the default value for the TLS protocol version (which is used for encrypted wss connections only).
|
void |
setTrustManagers(javax.net.ssl.TrustManager[] trustManagers)
Set a specific trust manager for the TLS (wss) protocol.
|
public static final java.lang.String PROTOCOL_WS
public static final java.lang.String PROTOCOL_WSS
public static final int CONNECT_STEP_NO_STEP
connect()
,
Constant Field Valuespublic static final int CONNECT_STEP_PREPARE
public static final int CONNECT_STEP_TCP_CONNECT
public static final int CONNECT_STEP_SSL_HANDSHAKE
public static final int CONNECT_STEP_HTTP_SEND_UPGRADE_REQUEST
public static final int CONNECT_STEP_HTTP_RECEIVE_UPGRADE_RESPONSE
public static final int CONNECT_STEP_HTTP_VERIFY_UPGRADE_RESPONSE
public static final int CONNECT_STEP_OPEN
public static final int CONNECT_STEP_CLOSED
public static final java.lang.String DEFAULT_TLS_PROTOCOL_VERSION
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS
public static final long DEFAULT_FRAME_SEND_TIMEOUT_MILLIS
public WebSocketClient(java.lang.String serverHost, int serverPort, java.lang.String urlPath, WebSocketClientListenerInterface clientListener)
serverHost
- the server name or IP addressserverPort
- the TLS port of the serverurlPath
- the URL path, example: /product/websocket-handlingclientListener
- the client listener, which has to be implemented by the caller of this classsetProtocol(String)
,
setConnectTimeoutMillis(int)
,
setFrameSendTimeoutMillis(long)
,
connect()
,
WebSocketClientContext.sendTextFrame(String)
,
WebSocketClientContext.sendBinaryFrame(byte[])
,
disconnect(long)
public void setProtocol(java.lang.String protocol)
protocol
- the protocol (ws or wss)PROTOCOL_WS
,
PROTOCOL_WSS
public java.lang.String getProtocol()
PROTOCOL_WS
,
PROTOCOL_WSS
public void setTLSProtocolVersion(java.lang.String tlsProtocolVersion)
tlsProtocolVersion
- the TLS protocol version. Supported values are:DEFAULT_TLS_PROTOCOL_VERSION
public java.lang.String getTLSProtocolVersion()
public void setTLSClientAuthenticationKeyManagers(javax.net.ssl.KeyManager[] tlsClientAuthenticationKeyManagers)
tlsClientAuthenticationKeyManagers
- the TLS client authentication credentialLoadPKCS12Certificate
,
LoadPKCS12Certificate.getKeyManagers()
public javax.net.ssl.KeyManager[] getTLSClientAuthenticationKeyManagers()
setTLSClientAuthenticationKeyManagers(KeyManager[])
public void setTrustManagers(javax.net.ssl.TrustManager[] trustManagers)
trustManagers
- the sources of peer authentication trust decisionspublic void setConnectTimeoutMillis(int connectTimeoutMillis)
connectTimeoutMillis
- the applied socket connect timeout in millisecondsDEFAULT_CONNECT_TIMEOUT_MILLIS
public int getConnectTimeoutMillis()
public void setFrameSendTimeoutMillis(long frameSendTimeoutMillis)
frameSendTimeoutMillis
- the applied frame send timeout in millisecondsDEFAULT_FRAME_SEND_TIMEOUT_MILLIS
public long getFrameSendTimeoutMillis()
public void setHttpOrigin(java.lang.String httpOrigin)
httpOrigin
- the value of the HTTP origin header fieldpublic void addHttpBasicAuthentication(java.lang.String username, java.lang.String password)
username
- the usernamepassword
- the passwordpublic void addHttpCookie(java.lang.String cookieName, java.lang.String cookieValue)
cookieName
- the name of the cookie to addcookieValue
- the value of the cookie to addpublic void addHttpHeaderField(java.lang.String httpHeaderField)
httpHeaderField
- the HTTP header field to add. Example: "Referer: http://en.wikipedia.org/wiki/Main_Page"public void setDebugToStdout()
public WebSocketClientContext connect() throws java.security.NoSuchAlgorithmException, java.security.KeyManagementException, java.io.IOException, WebSocketClientConnectException
java.security.NoSuchAlgorithmException
- if the TLS V1.2 protocol is not supportedjava.security.KeyManagementException
- if the SSL/TLS context cannot initializedjava.io.IOException
- if an IO error occursWebSocketClientConnectException
- if the HTTP upgrade request did fail or when a WebSocket connect timout occurred.public java.util.ArrayList<java.lang.String> getRequestHeader()
connect()
public java.util.ArrayList<java.lang.String> getResponseHeader()
public void disconnect(long serverConfirmCloseMillis) throws java.io.IOException, java.lang.InterruptedException
serverConfirmCloseMillis
- the wait time to receive the close frame confirmation from the server.java.io.IOException
- if somewhat failsjava.lang.InterruptedException
- if the current thread is interruptedpublic void printDebug(java.lang.String message)
message
- the debug messagesetDebugToStdout()
public long getTcpIpConnectTime()
public long getSslHandshakeTime()
public long getHttpUpgradeRequestTime()
public long getHttpUpgradeResponseTime()
public long getWebSocketOpenTime()
getTcpIpConnectTime()
,
getSslHandshakeTime()
,
getHttpUpgradeRequestTime()
,
getHttpUpgradeResponseTime()
public int getCurrentConnectStep()
CONNECT_STEP_NO_STEP
,
CONNECT_STEP_PREPARE
,
CONNECT_STEP_TCP_CONNECT
,
CONNECT_STEP_SSL_HANDSHAKE
,
CONNECT_STEP_HTTP_SEND_UPGRADE_REQUEST
,
CONNECT_STEP_HTTP_RECEIVE_UPGRADE_RESPONSE
,
CONNECT_STEP_HTTP_VERIFY_UPGRADE_RESPONSE
,
CONNECT_STEP_OPEN
,
CONNECT_STEP_CLOSED
,
currentConnectStepToString(int)
public static java.lang.String currentConnectStepToString(int connectStep)
connectStep
- the current internal state of the WebSocket connectiongetCurrentConnectStep()
public long getNumberOfPendingFramesToSend()
public long getTotalFramesSent()
public long getTotalBytesSent()
public long getTotalPayloadSent()
public long getTotalFramesReceived()
public long getTotalBytesReceived()
public long getTotalPayloadReceived()