public abstract class WebSocketClient extends AbstractWebSocket implements Runnable, WebSocket
connect()
, then receive events like onMessage(String)
via the overloaded
methods and to send(String)
data to the server.Modifier and Type | Field and Description |
---|---|
protected URI |
uri
The URI this channel is supposed to connect to.
|
Constructor and Description |
---|
WebSocketClient(URI serverUri)
Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
|
WebSocketClient(URI serverUri,
Draft protocolDraft)
Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
|
WebSocketClient(URI serverUri,
Draft protocolDraft,
Map<String,String> httpHeaders)
Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
|
WebSocketClient(URI serverUri,
Draft protocolDraft,
Map<String,String> httpHeaders,
int connectTimeout)
Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
|
WebSocketClient(URI serverUri,
Map<String,String> httpHeaders)
Constructs a WebSocketClient instance and sets it to the connect to the specified URI.
|
Modifier and Type | Method and Description |
---|---|
void |
addHeader(String key,
String value) |
void |
clearHeaders() |
void |
close()
Initiates the websocket close handshake.
|
void |
close(int code)
sends the closing handshake. may be send in response to an other handshake.
|
void |
close(int code,
String message)
sends the closing handshake. may be send in response to an other handshake.
|
void |
closeBlocking()
Same as
close but blocks until the websocket closed or failed to do so. |
void |
closeConnection(int code,
String message)
This will close the connection immediately without a proper close handshake.
|
void |
connect()
Initiates the websocket connection.
|
boolean |
connectBlocking()
Same as
connect but blocks until the websocket connected or failed to do so. |
boolean |
connectBlocking(long timeout,
TimeUnit timeUnit)
Same as
connect but blocks with a timeout until the websocket connected or failed
to do so. |
<T> T |
getAttachment()
Getter for the connection attachment.
|
WebSocket |
getConnection()
Getter for the engine
|
protected Collection<WebSocket> |
getConnections()
Getter to get all the currently available connections
|
Draft |
getDraft()
Returns the protocol version this channel uses.
|
InetSocketAddress |
getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to, or
null if it is not
bound. |
InetSocketAddress |
getLocalSocketAddress(WebSocket conn) |
IProtocol |
getProtocol()
Returns the used Sec-WebSocket-Protocol for this websocket connection
|
ReadyState |
getReadyState()
This represents the state of the connection.
|
InetSocketAddress |
getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or
null if it is
unconnected. |
InetSocketAddress |
getRemoteSocketAddress(WebSocket conn) |
String |
getResourceDescriptor()
Returns the HTTP Request-URI as defined by http://tools.ietf.org/html/rfc2616#section-5.1.2
If the opening handshake has not yet happened it will return null. |
Socket |
getSocket()
Returns the socket to allow Hostname Verification
|
SSLSession |
getSSLSession()
Returns the ssl session of websocket, if ssl/wss is used for this instance.
|
URI |
getURI()
Returns the URI that this WebSocketClient is connected to.
|
boolean |
hasBufferedData()
Checks if the websocket has buffered data
|
boolean |
hasSSLSupport()
Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection
|
boolean |
isClosed()
Is the websocket in the state CLOSED
|
boolean |
isClosing()
Is the websocket in the state CLOSING
|
boolean |
isFlushAndClose()
Returns true when no further frames may be submitted
This happens before the socket connection is closed. |
boolean |
isOpen()
Is the websocket in the state OPEN
|
abstract void |
onClose(int code,
String reason,
boolean remote)
Called after the websocket connection has been closed.
|
void |
onCloseInitiated(int code,
String reason)
Send when this peer sends a close handshake
|
void |
onClosing(int code,
String reason,
boolean remote)
Called as soon as no further frames are accepted
|
abstract void |
onError(Exception ex)
Called when errors occurs.
|
void |
onMessage(ByteBuffer bytes)
Callback for binary messages received from the remote host
|
abstract void |
onMessage(String message)
Callback for string messages received from the remote host
|
abstract void |
onOpen(ServerHandshake handshakedata)
Called after an opening handshake has been performed and the given websocket is ready to be
written on.
|
protected void |
onSetSSLParameters(SSLParameters sslParameters)
Apply specific SSLParameters If you override this method make sure to always call
super.onSetSSLParameters() to ensure the hostname validation is active
|
void |
onWebsocketClose(WebSocket conn,
int code,
String reason,
boolean remote)
Calls subclass' implementation of onClose.
|
void |
onWebsocketCloseInitiated(WebSocket conn,
int code,
String reason)
send when this peer sends a close handshake
|
void |
onWebsocketClosing(WebSocket conn,
int code,
String reason,
boolean remote)
Called as soon as no further frames are accepted
|
void |
onWebsocketError(WebSocket conn,
Exception ex)
Calls subclass' implementation of onIOError.
|
void |
onWebsocketMessage(WebSocket conn,
ByteBuffer blob)
Called when an entire binary frame has been received.
|
void |
onWebsocketMessage(WebSocket conn,
String message)
Calls subclass' implementation of onMessage.
|
void |
onWebsocketOpen(WebSocket conn,
Handshakedata handshake)
Calls subclass' implementation of onOpen.
|
void |
onWriteDemand(WebSocket conn)
This method is used to inform the selector thread that there is data queued to be written to
the socket.
|
void |
reconnect()
Reinitiates the websocket connection.
|
boolean |
reconnectBlocking()
Same as
reconnect but blocks until the websocket reconnected or failed to do
so. |
String |
removeHeader(String key) |
void |
run() |
void |
send(byte[] data)
Sends binary data to the connected webSocket server.
|
void |
send(ByteBuffer bytes)
Send Binary data (plain bytes) to the other end.
|
void |
send(String text)
Sends text to the connected websocket server.
|
void |
sendFragmentedFrame(Opcode op,
ByteBuffer buffer,
boolean fin)
Allows to send continuous/fragmented frames conveniently.
|
void |
sendFrame(Collection<Framedata> frames)
Send a collection of frames to the other end
|
void |
sendFrame(Framedata framedata)
Send a frame to the other end
|
void |
sendPing()
Send a ping to the other end
|
<T> void |
setAttachment(T attachment)
Setter for an attachment on the socket connection.
|
void |
setDnsResolver(DnsResolver dnsResolver)
Sets a custom DNS resolver.
|
void |
setProxy(Proxy proxy)
Method to set a proxy for this connection
|
void |
setSocket(Socket socket)
Deprecated.
use setSocketFactory
|
void |
setSocketFactory(SocketFactory socketFactory)
Accepts a SocketFactory.
|
getConnectionLostTimeout, isReuseAddr, isTcpNoDelay, setConnectionLostTimeout, setReuseAddr, setTcpNoDelay, startConnectionLostTimer, stopConnectionLostTimer
onPreparePing, onWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeReceivedAsServer, onWebsocketHandshakeSentAsClient, onWebsocketPing, onWebsocketPong
protected URI uri
public WebSocketClient(URI serverUri)
serverUri
- the server URI to connect topublic WebSocketClient(URI serverUri, Draft protocolDraft)
serverUri
- the server URI to connect toprotocolDraft
- The draft which should be used for this connectionpublic WebSocketClient(URI serverUri, Map<String,String> httpHeaders)
serverUri
- the server URI to connect tohttpHeaders
- Additional HTTP-Headerspublic WebSocketClient(URI serverUri, Draft protocolDraft, Map<String,String> httpHeaders)
serverUri
- the server URI to connect toprotocolDraft
- The draft which should be used for this connectionhttpHeaders
- Additional HTTP-Headerspublic WebSocketClient(URI serverUri, Draft protocolDraft, Map<String,String> httpHeaders, int connectTimeout)
serverUri
- the server URI to connect toprotocolDraft
- The draft which should be used for this connectionhttpHeaders
- Additional HTTP-HeadersconnectTimeout
- The Timeout for the connectionpublic URI getURI()
public Draft getDraft()
public Socket getSocket()
public void addHeader(String key, String value)
key
- Name of the header to add.value
- Value of the header to add.public String removeHeader(String key)
key
- Name of the header to remove.public void clearHeaders()
public void setDnsResolver(DnsResolver dnsResolver)
dnsResolver
- The DnsResolver to use.public void reconnect()
public boolean reconnectBlocking() throws InterruptedException
reconnect
but blocks until the websocket reconnected or failed to do
so.InterruptedException
- Thrown when the threads get interruptedpublic void connect()
public boolean connectBlocking() throws InterruptedException
connect
but blocks until the websocket connected or failed to do so.InterruptedException
- Thrown when the threads get interruptedpublic boolean connectBlocking(long timeout, TimeUnit timeUnit) throws InterruptedException
connect
but blocks with a timeout until the websocket connected or failed
to do so.timeout
- The connect timeouttimeUnit
- The timeout time unitInterruptedException
- Thrown when the threads get interruptedpublic void close()
closeBlocking
public void closeBlocking() throws InterruptedException
close
but blocks until the websocket closed or failed to do so.InterruptedException
- Thrown when the threads get interruptedpublic void send(String text)
public void send(byte[] data)
public <T> T getAttachment()
WebSocket
getAttachment
in interface WebSocket
T
- The type of the attachmentpublic <T> void setAttachment(T attachment)
WebSocket
setAttachment
in interface WebSocket
T
- The type of the attachmentattachment
- The object to be attached to the userprotected Collection<WebSocket> getConnections()
AbstractWebSocket
getConnections
in class AbstractWebSocket
public void sendPing()
WebSocket
protected void onSetSSLParameters(SSLParameters sslParameters)
sslParameters
- the SSLParameters which will be used for the SSLSocketpublic ReadyState getReadyState()
getReadyState
in interface WebSocket
public final void onWebsocketMessage(WebSocket conn, String message)
onWebsocketMessage
in interface WebSocketListener
conn
- The WebSocket instance this event is occurring on.message
- The UTF-8 decoded message that was received.public final void onWebsocketMessage(WebSocket conn, ByteBuffer blob)
WebSocketListener
onWebsocketMessage
in interface WebSocketListener
conn
- The WebSocket instance this event is occurring on.blob
- The binary message that was received.public final void onWebsocketOpen(WebSocket conn, Handshakedata handshake)
onWebsocketOpen
in interface WebSocketListener
conn
- The WebSocket instance this event is occurring on.handshake
- The handshake of the websocket instancepublic final void onWebsocketClose(WebSocket conn, int code, String reason, boolean remote)
onWebsocketClose
in interface WebSocketListener
conn
- The WebSocket instance this event is occurring on.code
- The codes can be looked up here: CloseFrame
reason
- Additional information stringremote
- Returns whether or not the closing of the connection was initiated by the remote
host.public final void onWebsocketError(WebSocket conn, Exception ex)
onWebsocketError
in interface WebSocketListener
conn
- The WebSocket instance this event is occurring on.ex
- The exception that occurred. public final void onWriteDemand(WebSocket conn)
WebSocketListener
onWriteDemand
in interface WebSocketListener
conn
- The WebSocket instance this event is occurring on.public void onWebsocketCloseInitiated(WebSocket conn, int code, String reason)
WebSocketListener
onWebsocketCloseInitiated
in interface WebSocketListener
conn
- The WebSocket instance this event is occurring on.code
- The codes can be looked up here: CloseFrame
reason
- Additional information stringpublic void onWebsocketClosing(WebSocket conn, int code, String reason, boolean remote)
WebSocketListener
onWebsocketClosing
in interface WebSocketListener
conn
- The WebSocket instance this event is occurring on.code
- The codes can be looked up here: CloseFrame
reason
- Additional information stringremote
- Returns whether or not the closing of the connection was initiated by the remote
host.public void onCloseInitiated(int code, String reason)
code
- The codes can be looked up here: CloseFrame
reason
- Additional information stringpublic void onClosing(int code, String reason, boolean remote)
code
- The codes can be looked up here: CloseFrame
reason
- Additional information stringremote
- Returns whether or not the closing of the connection was initiated by the remote
host.public WebSocket getConnection()
public InetSocketAddress getLocalSocketAddress(WebSocket conn)
getLocalSocketAddress
in interface WebSocketListener
conn
- The WebSocket instance this event is occurring on.WebSocket.getLocalSocketAddress()
public InetSocketAddress getRemoteSocketAddress(WebSocket conn)
getRemoteSocketAddress
in interface WebSocketListener
conn
- The WebSocket instance this event is occurring on.null
if it
is unconnected.WebSocket.getRemoteSocketAddress()
public abstract void onOpen(ServerHandshake handshakedata)
handshakedata
- The handshake of the websocket instancepublic abstract void onMessage(String message)
message
- The UTF-8 decoded message that was received.onMessage(ByteBuffer)
public abstract void onClose(int code, String reason, boolean remote)
code
- The codes can be looked up here: CloseFrame
reason
- Additional information stringremote
- Returns whether or not the closing of the connection was initiated by the remote
host.public abstract void onError(Exception ex)
onClose(int, String, boolean)
will be called additionally.ex
- The exception causing this errorpublic void onMessage(ByteBuffer bytes)
bytes
- The binary message that was received.onMessage(String)
public void setProxy(Proxy proxy)
proxy
- the proxy to use for this websocket client@Deprecated public void setSocket(Socket socket)
connect
.
If the given socket is not yet bound it will be bound to the uri specified in the constructor.socket
- The socket which should be used for the connectionpublic void setSocketFactory(SocketFactory socketFactory)
connect
. The socket
will be bound to the uri specified in the constructor.socketFactory
- The socket factory which should be used for the connection.public void sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin)
WebSocket
If the first frame you send is also the last then it is not a fragmented frame and will received via onMessage instead of onFragmented even though it was send by this method.
sendFragmentedFrame
in interface WebSocket
op
- This is only important for the first frame in the sequence. Opcode.TEXT,
Opcode.BINARY are allowed.buffer
- The buffer which contains the payload. It may have no bytes remaining.fin
- true means the current frame is the last in the sequence.public boolean isOpen()
WebSocket
public boolean isFlushAndClose()
WebSocket
isFlushAndClose
in interface WebSocket
public boolean isClosed()
WebSocket
public boolean isClosing()
WebSocket
public boolean hasBufferedData()
WebSocket
hasBufferedData
in interface WebSocket
public void close(int code)
WebSocket
public void close(int code, String message)
WebSocket
public void closeConnection(int code, String message)
WebSocket
closeConnection
in interface WebSocket
code
- the closing codemessage
- the closing messagepublic void send(ByteBuffer bytes)
WebSocket
public void sendFrame(Framedata framedata)
WebSocket
public void sendFrame(Collection<Framedata> frames)
WebSocket
public InetSocketAddress getLocalSocketAddress()
WebSocket
null
if it is not
bound.getLocalSocketAddress
in interface WebSocket
public InetSocketAddress getRemoteSocketAddress()
WebSocket
null
if it is
unconnected.getRemoteSocketAddress
in interface WebSocket
public String getResourceDescriptor()
WebSocket
getResourceDescriptor
in interface WebSocket
public boolean hasSSLSupport()
WebSocket
hasSSLSupport
in interface WebSocket
public SSLSession getSSLSession()
WebSocket
getSSLSession
in interface WebSocket
public IProtocol getProtocol()
WebSocket
getProtocol
in interface WebSocket
Copyright © 2022. All rights reserved.