StreamingClient
public class WSClient extends java.lang.Object implements StreamingClient
HTTPStreamClient
that simplifies it for use with WebSockets.Modifier and Type | Class | Description |
---|---|---|
static interface |
WSClient.WebSocketDataReader |
This is the Read callback used for
WSClient . |
Modifier and Type | Field | Description |
---|---|---|
static org.threadly.litesockets.protocols.http.request.HTTPRequest |
DEFAULT_WS_REQUEST |
|
static org.threadly.litesockets.protocols.http.response.HTTPResponse |
DEFAULT_WS_RESPONSE |
|
static int |
WS_PORT |
|
static java.lang.String |
WS_STRING |
|
static int |
WSS_PORT |
|
static java.lang.String |
WSS_STRING |
Constructor | Description |
---|---|
WSClient(org.threadly.litesockets.SocketExecuter se,
java.lang.String host,
int port) |
Creates a websocket connection to the specified host and port, using the provided
SocketExecuter . |
WSClient(org.threadly.litesockets.SocketExecuter se,
java.net.URI uri) |
Creates a websocket connection to the specified
URI . |
WSClient(org.threadly.litesockets.TCPClient client) |
This takes over an existing TCPClient to do websocket communications.
|
Modifier and Type | Method | Description |
---|---|---|
void |
addCloseListener(java.lang.Runnable cl) |
Sets a runnable to be ran once this connection is closed.
|
void |
close() |
This will close the client.
|
org.threadly.concurrent.future.ListenableFuture<java.lang.Boolean> |
connect() |
This is called to connect this client to the server.
|
void |
doPingAutoPong(boolean doPings) |
This sets whether or not the WebSocketClient will auto replay to websocket pings.
|
void |
enableSSL() |
This is called to enable ssl on this connection.
|
void |
enableSSL(javax.net.ssl.SSLEngine ssle) |
This is called to enable ssl on this connection.
|
java.util.concurrent.Executor |
getClientsThreadExecutor() |
Gets the
Executor for this client. |
boolean |
getDefaultMask() |
Returns the value of the default mask set on this WebSocketClient.
|
org.threadly.litesockets.protocols.websocket.WSOPCode |
getDefaultOpCode() |
Returns the current default
WSOPCode in use by this connection. |
org.threadly.concurrent.future.ListenableFuture<?> |
getLastWriteFuture() |
|
boolean |
getPingAutoReply() |
Returns the current value set for pingAuto reply.
|
boolean |
isConnected() |
|
void |
setConnectionTimeout(int timeout) |
This sets the timeout on the connection to the remote host.
|
void |
setDefaultMask(boolean mask) |
This sets whether or not the default
write(ByteBuffer) should mask the data that is being sent. |
void |
setDefaultOpCode(org.threadly.litesockets.protocols.websocket.WSOPCode wsoc) |
Sets the default
WSOPCode to use when calling write(ByteBuffer) . |
void |
setExtraHeader(java.lang.String key,
java.lang.String value) |
This allows you to set extra headers or change any header on the HTTP upgrade request sent to the server.
|
void |
setRequestPath(java.lang.String path) |
This allows you to set the path used in the initial HTTP upgrade Request sent to the WebSocket server.
|
void |
setRequestResponseHeaders(org.threadly.litesockets.protocols.http.request.HTTPRequest httpRequest,
org.threadly.litesockets.protocols.http.response.HTTPResponse httpResponse,
boolean writeRequest) |
Sets the HTTPRequest and HTTPResponse allowing the streaming client to start streaming
w/o sending these in itself.
|
void |
setWebSocketDataReader(WSClient.WebSocketDataReader reader) |
Sets the
WSClient.WebSocketDataReader for this client. |
void |
setWebSocketKey(java.lang.String key) |
This allows you to set the websocket key used in the initial HTTP upgrade request sent to the server.
|
void |
setWebSocketVersion(int version) |
This allows you to set the websocket version used in the initial HTTP upgrade request.
|
org.threadly.concurrent.future.ListenableFuture<?> |
write(java.nio.ByteBuffer bb) |
This performs a write to the connection.
|
org.threadly.concurrent.future.ListenableFuture<?> |
write(java.nio.ByteBuffer bb,
org.threadly.litesockets.protocols.websocket.WSOPCode opCode,
boolean mask) |
This performs a write to the websocket connection.
|
public static final org.threadly.litesockets.protocols.http.response.HTTPResponse DEFAULT_WS_RESPONSE
public static final org.threadly.litesockets.protocols.http.request.HTTPRequest DEFAULT_WS_REQUEST
public static final java.lang.String WSS_STRING
public static final java.lang.String WS_STRING
public static final int WSS_PORT
public static final int WS_PORT
public WSClient(org.threadly.litesockets.TCPClient client)
client
- the TCPClient to use for this connection.public WSClient(org.threadly.litesockets.SocketExecuter se, java.net.URI uri) throws java.io.IOException
URI
.se
- the SocketExecuter
to use for creating this connection.uri
- the URI
to use for setting up this connection.java.io.IOException
- this is thrown if there are any problems creating the TCP socket for this connection.public WSClient(org.threadly.litesockets.SocketExecuter se, java.lang.String host, int port) throws java.io.IOException
SocketExecuter
.se
- the SocketExecuter
to use for this connection.host
- the host string or IP to make the connection too.port
- the port on the host to connect too.java.io.IOException
- this is thrown if there are any problems creating the tcp socket for this connection.public void enableSSL()
StreamingClient
enableSSL
in interface StreamingClient
public void enableSSL(javax.net.ssl.SSLEngine ssle)
StreamingClient
SSLEngine
to use for this connection allow you to validate the certificates if needed.enableSSL
in interface StreamingClient
ssle
- the SSLEngine
to use for this connection.public void setConnectionTimeout(int timeout)
StreamingClient
StreamingClient.connect()
is called.setConnectionTimeout
in interface StreamingClient
timeout
- the timeout in milliseconds to use for this connection.public void setDefaultMask(boolean mask)
write(ByteBuffer)
should mask the data that is being sent.mask
- true to set the default to mask the sent data, false to not mask the data.public boolean getDefaultMask()
public void doPingAutoPong(boolean doPings)
onData(WSFrame, ByteBuffer)
call will get the pings and they must be handled manually.doPings
- set to true if the WebSocketClient should automatically reply to pings, false to handle them manually.public boolean getPingAutoReply()
public void setDefaultOpCode(org.threadly.litesockets.protocols.websocket.WSOPCode wsoc)
WSOPCode
to use when calling write(ByteBuffer)
.
Only standard WebSocket OpCodes can be used as a "default" to use anything other then the
standard OpCodes use the write(ByteBuffer, WSOPCode, boolean)
method.wsoc
- the default WSOPCode
to use on this connection.public org.threadly.litesockets.protocols.websocket.WSOPCode getDefaultOpCode()
WSOPCode
in use by this connection.WSOPCode
currently used by default.public void setRequestPath(java.lang.String path)
connect()
is called.path
- the path to use when doing the initial HTTP Request.public void setWebSocketKey(java.lang.String key)
connect()
is called.key
- the key to use for the upgrade request.public void setWebSocketVersion(int version)
connect()
is called.version
- the version to use.public void setExtraHeader(java.lang.String key, java.lang.String value)
connect()
is called.
NOTE: You can override any header here, make sure you know what your doing!key
- The HTTP HeaderKey.value
- The HTTP HeaderValue.public void setWebSocketDataReader(WSClient.WebSocketDataReader reader)
WSClient.WebSocketDataReader
for this client. This will be used for callbacks when full
websocket frames are received. These call backs will happen in order and in a thread safe way (per client).
NOTE: this can be set to null but if it is all reads for this client will be blocked till its set again.reader
- the WSClient.WebSocketDataReader
to use for this client.public void setRequestResponseHeaders(org.threadly.litesockets.protocols.http.request.HTTPRequest httpRequest, org.threadly.litesockets.protocols.http.response.HTTPResponse httpResponse, boolean writeRequest)
StreamingClient
setRequestResponseHeaders
in interface StreamingClient
public org.threadly.concurrent.future.ListenableFuture<?> write(java.nio.ByteBuffer bb)
StreamingClient
write
in interface StreamingClient
bb
- the ByteBuffer
to write to socket.ListenableFuture
that will be completed once the frame has been fully written to the socket.public java.util.concurrent.Executor getClientsThreadExecutor()
StreamingClient
Executor
for this client.getClientsThreadExecutor
in interface StreamingClient
Executor
for this client.public org.threadly.concurrent.future.ListenableFuture<?> write(java.nio.ByteBuffer bb, org.threadly.litesockets.protocols.websocket.WSOPCode opCode, boolean mask)
ByteBuffer
written is seen as an individual websocketFrame.bb
- the ByteBuffer
to write to frame and write to the websocket.opCode
- the opCode to use in the websocket frame.mask
- sets whether or not to mask the websocket data. true to mask, false to not.ListenableFuture
that will be completed once the frame has been fully written to the socket.public org.threadly.concurrent.future.ListenableFuture<?> getLastWriteFuture()
getLastWriteFuture
in interface StreamingClient
public org.threadly.concurrent.future.ListenableFuture<java.lang.Boolean> connect()
StreamingClient
connect
in interface StreamingClient
ListenableFuture
that will be completed when the upgrade is complete, if the upgrade fails the a ListenableFuture
be failed.public boolean isConnected()
isConnected
in interface StreamingClient
public void close()
StreamingClient
close
in interface StreamingClient
public void addCloseListener(java.lang.Runnable cl)
StreamingClient
addCloseListener
in interface StreamingClient
cl
- the runnable to run on the connection close.