-
- All Implemented Interfaces:
-
com.tds.common.websocket.conn.WebSocket
,com.tds.common.websocket.conn.WebSocketListener
,java.lang.Runnable
public abstract class WebSocketClient extends AbstractWebSocket implements Runnable, WebSocket
-
-
Field Summary
Fields Modifier and Type Field Description protected URI
uri
private Socket
socket
private SocketFactory
socketFactory
private Proxy
proxy
private Draft
draft
private DnsResolver
dnsResolver
-
Constructor Summary
Constructors Constructor 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, 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)
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.
-
Method Summary
Modifier and Type Method Description URI
getURI()
Returns the URI that this WebSocketClient is connected to. Socket
getSocket()
Returns the socket to allow Hostname Verification void
setSocket(Socket socket)
Accepts bound and unbound sockets. void
setSocketFactory(SocketFactory socketFactory)
Accepts a SocketFactory. void
setProxy(Proxy proxy)
Method to set a proxy for this connection Draft
getDraft()
Returns the protocol version this channel uses. void
setDnsResolver(DnsResolver dnsResolver)
Sets a custom DNS resolver. void
addHeader(String key, String value)
String
removeHeader(String key)
void
clearHeaders()
void
reconnect()
Reinitiates the websocket connection. boolean
reconnectBlocking()
Same as reconnect
but blocks until the websocket reconnected or failed to doso.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 failedto do so.void
close()
Initiates the websocket close handshake. void
closeBlocking()
Same as close
but blocks until the websocket closed or failed to do so.void
send(String text)
Sends text to the connected websocket server. void
send(Array<byte> data)
Sends binary data to the connected webSocket server. <T> T
getAttachment()
Getter for the connection attachment. <T> void
setAttachment(T attachment)
Setter for an attachment on the socket connection. void
sendPing()
Send a ping to the other end void
run()
ReadyState
getReadyState()
This represents the state of the connection. final void
onWebsocketMessage(WebSocket conn, String message)
Calls subclass' implementation of onMessage. final void
onWebsocketMessage(WebSocket conn, ByteBuffer blob)
Called when an entire binary frame has been received. final void
onWebsocketOpen(WebSocket conn, Handshakedata handshake)
Calls subclass' implementation of onOpen. final void
onWebsocketClose(WebSocket conn, int code, String reason, boolean remote)
Calls subclass' implementation of onClose. final void
onWebsocketError(WebSocket conn, Exception ex)
Calls subclass' implementation of onIOError. final void
onWriteDemand(WebSocket conn)
This method is used to inform the selector thread that there is data queued to be written tothe socket. 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
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 WebSocket
getConnection()
Getter for the engine InetSocketAddress
getLocalSocketAddress(WebSocket conn)
InetSocketAddress
getRemoteSocketAddress(WebSocket conn)
abstract void
onOpen(ServerHandshake handshakedata)
Called after an opening handshake has been performed and the given websocket is ready to bewritten on. abstract void
onMessage(String message)
Callback for string messages received from the remote host abstract void
onClose(int code, String reason, boolean remote)
Called after the websocket connection has been closed. abstract void
onError(Exception ex)
Called when errors occurs. void
onMessage(ByteBuffer bytes)
Callback for binary messages received from the remote host void
sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin)
Allows to send continuous/fragmented frames conveniently. boolean
isOpen()
Is the websocket in the state OPEN boolean
isFlushAndClose()
Returns true when no further frames may be submitted This happens before the socketconnection is closed. boolean
isClosed()
Is the websocket in the state CLOSED boolean
isClosing()
Is the websocket in the state CLOSING boolean
hasBufferedData()
Checks if the websocket has buffered data void
close(int code)
sends the closing handshake. void
close(int code, String message)
sends the closing handshake. void
closeConnection(int code, String message)
This will close the connection immediately without a proper close handshake. void
send(ByteBuffer bytes)
Send Binary data (plain bytes) to the other end. void
sendFrame(Framedata framedata)
Send a frame to the other end void
sendFrame(Collection<Framedata> frames)
Send a collection of frames to the other end InetSocketAddress
getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to. InetSocketAddress
getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or {@code null}
if it isunconnected.String
getResourceDescriptor()
Returns the HTTP Request-URI as defined by http://tools.ietf.org/html/rfc2616#section-5.1. boolean
hasSSLSupport()
Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection SSLSession
getSSLSession()
Returns the ssl session of websocket, if ssl/wss is used for this instance. IProtocol
getProtocol()
Returns the used Sec-WebSocket-Protocol for this websocket connection -
Methods inherited from class com.tds.common.websocket.conn.AbstractWebSocket
getConnectionLostTimeout, isReuseAddr, isTcpNoDelay, setConnectionLostTimeout, setReuseAddr, setTcpNoDelay
-
Methods inherited from class com.tds.common.websocket.conn.WebSocketAdapter
onPreparePing, onWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeReceivedAsServer, onWebsocketHandshakeSentAsClient, onWebsocketPing, onWebsocketPong
-
Methods inherited from class com.tds.common.websocket.conn.WebSocketListener
onWebsocketClose, onWebsocketCloseInitiated, onWebsocketClosing, onWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeReceivedAsServer, onWebsocketOpen
-
Methods inherited from class java.lang.Runnable
run
-
Methods inherited from class com.tds.common.websocket.conn.WebSocket
send
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Constructor Detail
-
WebSocketClient
WebSocketClient(URI serverUri)
Constructs a WebSocketClient instance and sets it to the connect to the specified URI.- Parameters:
serverUri
- the server URI to connect to
-
WebSocketClient
WebSocketClient(URI serverUri, Draft protocolDraft)
Constructs a WebSocketClient instance and sets it to the connect to the specified URI.- Parameters:
serverUri
- the server URI to connect toprotocolDraft
- The draft which should be used for this connection
-
WebSocketClient
WebSocketClient(URI serverUri, Map<String, String> httpHeaders)
Constructs a WebSocketClient instance and sets it to the connect to the specified URI.- Parameters:
serverUri
- the server URI to connect tohttpHeaders
- Additional HTTP-Headers
-
WebSocketClient
WebSocketClient(URI serverUri, Draft protocolDraft, Map<String, String> httpHeaders)
Constructs a WebSocketClient instance and sets it to the connect to the specified URI.- Parameters:
serverUri
- the server URI to connect toprotocolDraft
- The draft which should be used for this connectionhttpHeaders
- Additional HTTP-Headers
-
WebSocketClient
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.- Parameters:
serverUri
- the server URI to connect toprotocolDraft
- The draft which should be used for this connectionhttpHeaders
- Additional HTTP-HeadersconnectTimeout
- The Timeout for the connection
-
-
Method Detail
-
setSocket
@Deprecated() void setSocket(Socket socket)
Accepts bound and unbound sockets. This method must be called before
connect
.If the given socket is not yet bound it will be bound to the uri specified in the constructor.- Parameters:
socket
- The socket which should be used for the connection
-
setSocketFactory
void setSocketFactory(SocketFactory socketFactory)
Accepts a SocketFactory. This method must be called before
connect
. The socketwill be bound to the uri specified in the constructor.- Parameters:
socketFactory
- The socket factory which should be used for the connection.
-
setProxy
void setProxy(Proxy proxy)
Method to set a proxy for this connection
- Parameters:
proxy
- the proxy to use for this websocket client
-
getDraft
Draft getDraft()
Returns the protocol version this channel uses. For more infos seehttps://github.com/TooTallNate/Java-WebSocket/wiki/Drafts
-
setDnsResolver
void setDnsResolver(DnsResolver dnsResolver)
Sets a custom DNS resolver.
- Parameters:
dnsResolver
- The DnsResolver to use.
-
addHeader
void addHeader(String key, String value)
- Parameters:
key
- Name of the header to add.value
- Value of the header to add.
-
removeHeader
String removeHeader(String key)
- Parameters:
key
- Name of the header to remove.
-
clearHeaders
void clearHeaders()
-
reconnect
void reconnect()
Reinitiates the websocket connection. This method does not block.
-
reconnectBlocking
boolean reconnectBlocking()
Same as
reconnect
but blocks until the websocket reconnected or failed to doso.
-
connect
void connect()
Initiates the websocket connection. This method does not block.
-
connectBlocking
boolean connectBlocking()
Same as
connect
but blocks until the websocket connected or failed to do so.
-
connectBlocking
boolean connectBlocking(long timeout, TimeUnit timeUnit)
Same as
connect
but blocks with a timeout until the websocket connected or failedto do so.- Parameters:
timeout
- The connect timeouttimeUnit
- The timeout time unit
-
close
void close()
Initiates the websocket close handshake. This method does not block In oder to make surethe connection is closed use
closeBlocking
-
closeBlocking
void closeBlocking()
Same as
close
but blocks until the websocket closed or failed to do so.
-
send
void send(String text)
Sends text to the connected websocket server.
- Parameters:
text
- The string which will be transmitted.
-
send
void send(Array<byte> data)
Sends binary data to the connected webSocket server.
- Parameters:
data
- The byte-Array of data to send to the WebSocket server.
-
getAttachment
<T> T getAttachment()
Getter for the connection attachment.
-
setAttachment
<T> void setAttachment(T attachment)
Setter for an attachment on the socket connection. The attachment may be of any type.
- Parameters:
attachment
- The object to be attached to the user
-
sendPing
void sendPing()
Send a ping to the other end
-
run
void run()
-
getReadyState
ReadyState getReadyState()
This represents the state of the connection.
-
onWebsocketMessage
final void onWebsocketMessage(WebSocket conn, String message)
Calls subclass' implementation of onMessage.
-
onWebsocketMessage
final void onWebsocketMessage(WebSocket conn, ByteBuffer blob)
Called when an entire binary frame has been received. Do whatever you want here...
- Parameters:
conn
- The WebSocket instance this event is occurring on.blob
- The binary message that was received.
-
onWebsocketOpen
final void onWebsocketOpen(WebSocket conn, Handshakedata handshake)
Calls subclass' implementation of onOpen.
-
onWebsocketClose
final void onWebsocketClose(WebSocket conn, int code, String reason, boolean remote)
Calls subclass' implementation of onClose.
-
onWebsocketError
final void onWebsocketError(WebSocket conn, Exception ex)
Calls subclass' implementation of onIOError.
-
onWriteDemand
final void onWriteDemand(WebSocket conn)
This method is used to inform the selector thread that there is data queued to be written tothe socket.
- Parameters:
conn
- The WebSocket instance this event is occurring on.
-
onWebsocketCloseInitiated
void onWebsocketCloseInitiated(WebSocket conn, int code, String reason)
send when this peer sends a close handshake
- Parameters:
code
- The codes can be looked up here: CloseFramereason
- Additional information string
-
onWebsocketClosing
void onWebsocketClosing(WebSocket conn, int code, String reason, boolean remote)
Called as soon as no further frames are accepted
- Parameters:
code
- The codes can be looked up here: CloseFramereason
- Additional information stringremote
- Returns whether or not the closing of the connection was initiated by the remotehost.
-
onCloseInitiated
void onCloseInitiated(int code, String reason)
Send when this peer sends a close handshake
- Parameters:
code
- The codes can be looked up here: CloseFramereason
- Additional information string
-
onClosing
void onClosing(int code, String reason, boolean remote)
Called as soon as no further frames are accepted
- Parameters:
code
- The codes can be looked up here: CloseFramereason
- Additional information stringremote
- Returns whether or not the closing of the connection was initiated by the remotehost.
-
getConnection
WebSocket getConnection()
Getter for the engine
-
getLocalSocketAddress
InetSocketAddress getLocalSocketAddress(WebSocket conn)
- Parameters:
conn
- The WebSocket instance this event is occurring on.
-
getRemoteSocketAddress
InetSocketAddress getRemoteSocketAddress(WebSocket conn)
- Parameters:
conn
- The WebSocket instance this event is occurring on.
-
onOpen
abstract void onOpen(ServerHandshake handshakedata)
Called after an opening handshake has been performed and the given websocket is ready to bewritten on.
- Parameters:
handshakedata
- The handshake of the websocket instance
-
onMessage
abstract void onMessage(String message)
Callback for string messages received from the remote host
- Parameters:
message
- The UTF-8 decoded message that was received.
-
onClose
abstract void onClose(int code, String reason, boolean remote)
Called after the websocket connection has been closed.
- Parameters:
code
- The codes can be looked up here: CloseFramereason
- Additional information stringremote
- Returns whether or not the closing of the connection was initiated by the remotehost.
-
onError
abstract void onError(Exception ex)
Called when errors occurs. If an error causes the websocket connection to fail will be called additionally. This method will be calledprimarily because of IO or protocol errors. If the given exception is an RuntimeExceptionthat probably means that you encountered a bug.
- Parameters:
ex
- The exception causing this error
-
onMessage
void onMessage(ByteBuffer bytes)
Callback for binary messages received from the remote host
- Parameters:
bytes
- The binary message that was received.
-
sendFragmentedFrame
void sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin)
Allows to send continuous/fragmented frames conveniently. For more into on this frame typesee http://tools.ietf.org/html/rfc6455#section-5.4
If the first frame you send is also the last then it is not a fragmented frame and willreceived via onMessage instead of onFragmented even though it was send by this method.
- Parameters:
op
- This is only important for the first frame in the sequence.buffer
- The buffer which contains the payload.fin
- true means the current frame is the last in the sequence.
-
isOpen
boolean isOpen()
Is the websocket in the state OPEN
-
isFlushAndClose
boolean isFlushAndClose()
Returns true when no further frames may be submitted This happens before the socketconnection is closed.
-
isClosed
boolean isClosed()
Is the websocket in the state CLOSED
-
isClosing
boolean isClosing()
Is the websocket in the state CLOSING
-
hasBufferedData
boolean hasBufferedData()
Checks if the websocket has buffered data
-
close
void close(int code)
sends the closing handshake. may be send in response to an other handshake.
- Parameters:
code
- the closing code
-
close
void close(int code, String message)
sends the closing handshake. may be send in response to an other handshake.
- Parameters:
code
- the closing codemessage
- the closing message
-
closeConnection
void closeConnection(int code, String message)
This will close the connection immediately without a proper close handshake. The code and themessage therefore won't be transferred over the wire also they will be forwarded toonClose/onWebsocketClose.
- Parameters:
code
- the closing codemessage
- the closing message
-
send
void send(ByteBuffer bytes)
Send Binary data (plain bytes) to the other end.
- Parameters:
bytes
- the binary data to send
-
sendFrame
void sendFrame(Framedata framedata)
Send a frame to the other end
- Parameters:
framedata
- the frame to send to the other end
-
sendFrame
void sendFrame(Collection<Framedata> frames)
Send a collection of frames to the other end
- Parameters:
frames
- the frames to send to the other end
-
getLocalSocketAddress
InetSocketAddress getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to.
-
getRemoteSocketAddress
InetSocketAddress getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or
{@code null}
if it isunconnected.
-
getResourceDescriptor
String getResourceDescriptor()
Returns the HTTP Request-URI as defined by http://tools.ietf.org/html/rfc2616#section-5.1.2If the opening handshake has not yet happened it will return null.
-
hasSSLSupport
boolean hasSSLSupport()
Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection
-
getSSLSession
SSLSession getSSLSession()
Returns the ssl session of websocket, if ssl/wss is used for this instance.
-
getProtocol
IProtocol getProtocol()
Returns the used Sec-WebSocket-Protocol for this websocket connection
-
-
-
-