Package org.java_websocket.client
Class WebSocketClient
java.lang.Object
org.java_websocket.WebSocketAdapter
org.java_websocket.AbstractWebSocket
org.java_websocket.client.WebSocketClient
- All Implemented Interfaces:
Runnable
,WebSocket
,WebSocketListener
A subclass must implement at least onOpen, onClose, and
onMessage to be useful. At runtime the user is expected to establish a connection via
connect()
, then receive events like onMessage(String)
via the overloaded
methods and to send(String)
data to the server.-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionWebSocketClient
(URI serverUri) 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) Constructs a WebSocketClient instance and sets it to the connect to the specified URI.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 TypeMethodDescriptionvoid
void
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
sends the closing handshake. may be send in response to an other handshake.void
Same asclose
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
Same asconnect
but blocks until the websocket connected or failed to do so.boolean
connectBlocking
(long timeout, TimeUnit timeUnit) Same asconnect
but blocks with a timeout until the websocket connected or failed to do so.<T> T
Getter for the connection attachment.Getter for the engineprotected Collection<WebSocket>
Getter to get all the currently available connectionsgetDraft()
Returns the protocol version this channel uses.
For more infos see https://github.com/TooTallNate/Java-WebSocket/wiki/DraftsReturns the address of the endpoint this socket is bound to, ornull
if it is not bound.Returns the used Sec-WebSocket-Protocol for this websocket connectionThis represents the state of the connection.Returns the address of the endpoint this socket is connected to, ornull
if it is unconnected.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.Returns the socket to allow Hostname VerificationReturns the ssl session of websocket, if ssl/wss is used for this instance.getURI()
Returns the URI that this WebSocketClient is connected to.boolean
Checks if the websocket has buffered databoolean
Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connectionboolean
isClosed()
Is the websocket in the state CLOSEDboolean
Is the websocket in the state CLOSINGboolean
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 OPENabstract void
Called after the websocket connection has been closed.void
onCloseInitiated
(int code, String reason) Send when this peer sends a close handshakevoid
Called as soon as no further frames are acceptedabstract void
Called when errors occurs.abstract void
Callback for string messages received from the remote hostvoid
onMessage
(ByteBuffer bytes) Callback for binary messages received from the remote hostabstract 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 activefinal 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 handshakevoid
onWebsocketClosing
(WebSocket conn, int code, String reason, boolean remote) Called as soon as no further frames are acceptedfinal void
onWebsocketError
(WebSocket conn, Exception ex) Calls subclass' implementation of onIOError.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
onWriteDemand
(WebSocket conn) This method is used to inform the selector thread that there is data queued to be written to the socket.void
Reinitiates the websocket connection.boolean
Same asreconnect
but blocks until the websocket reconnected or failed to do so.removeHeader
(String key) void
run()
void
send
(byte[] data) Sends binary data to the connected webSocket server.void
Sends text to the connected websocket server.void
send
(ByteBuffer bytes) Send Binary data (plain bytes) to the other end.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 endvoid
Send a frame to the other endvoid
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
Method to set a proxy for this connectionvoid
Deprecated.use setSocketFactoryvoid
setSocketFactory
(SocketFactory socketFactory) Accepts a SocketFactory.
This method must be called beforeconnect
.Methods inherited from class org.java_websocket.AbstractWebSocket
getConnectionLostTimeout, isReuseAddr, isTcpNoDelay, setConnectionLostTimeout, setReuseAddr, setTcpNoDelay, startConnectionLostTimer, stopConnectionLostTimer
Methods inherited from class org.java_websocket.WebSocketAdapter
onPreparePing, onWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeReceivedAsServer, onWebsocketHandshakeSentAsClient, onWebsocketPing, onWebsocketPong
-
Field Details
-
uri
The URI this channel is supposed to connect to.
-
-
Constructor Details
-
WebSocketClient
Constructs a WebSocketClient instance and sets it to the connect to the specified URI. The channel does not attampt to connect automatically. The connection will be established once you call connect.- Parameters:
serverUri
- the server URI to connect to
-
WebSocketClient
Constructs a WebSocketClient instance and sets it to the connect to the specified URI. The channel does not attampt to connect automatically. The connection will be established once you call connect.- Parameters:
serverUri
- the server URI to connect toprotocolDraft
- The draft which should be used for this connection
-
WebSocketClient
Constructs a WebSocketClient instance and sets it to the connect to the specified URI. The channel does not attampt to connect automatically. The connection will be established once you call connect.- Parameters:
serverUri
- the server URI to connect tohttpHeaders
- Additional HTTP-Headers- Since:
- 1.3.8
-
WebSocketClient
Constructs a WebSocketClient instance and sets it to the connect to the specified URI. The channel does not attampt to connect automatically. The connection will be established once you call connect.- Parameters:
serverUri
- the server URI to connect toprotocolDraft
- The draft which should be used for this connectionhttpHeaders
- Additional HTTP-Headers- Since:
- 1.3.8
-
WebSocketClient
public 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. The channel does not attampt to connect automatically. The connection will be established once you call connect.- 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 Details
-
getURI
Returns the URI that this WebSocketClient is connected to.- Returns:
- the URI connected to
-
getDraft
Returns the protocol version this channel uses.
For more infos see https://github.com/TooTallNate/Java-WebSocket/wiki/Drafts -
getSocket
Returns the socket to allow Hostname Verification- Returns:
- the socket used for this connection
-
addHeader
- Parameters:
key
- Name of the header to add.value
- Value of the header to add.- Since:
- 1.4.1 Adds an additional header to be sent in the handshake.
If the connection is already made, adding headers has no effect, unless reconnect is called, which then a new handshake is sent.
If a header with the same key already exists, it is overridden.
-
removeHeader
- Parameters:
key
- Name of the header to remove.- Returns:
- the previous value associated with key, or null if there was no mapping for key.
- Since:
- 1.4.1 Removes a header from the handshake to be sent, if header key exists.
-
clearHeaders
public void clearHeaders()- Since:
- 1.4.1 Clears all previously put headers.
-
setDnsResolver
Sets a custom DNS resolver.- Parameters:
dnsResolver
- The DnsResolver to use.- Since:
- 1.4.1
-
reconnect
public void reconnect()Reinitiates the websocket connection. This method does not block.- Since:
- 1.3.8
-
reconnectBlocking
Same asreconnect
but blocks until the websocket reconnected or failed to do so.- Returns:
- Returns whether it succeeded or not.
- Throws:
InterruptedException
- Thrown when the threads get interrupted- Since:
- 1.3.8
-
connect
public void connect()Initiates the websocket connection. This method does not block. -
connectBlocking
Same asconnect
but blocks until the websocket connected or failed to do so.- Returns:
- Returns whether it succeeded or not.
- Throws:
InterruptedException
- Thrown when the threads get interrupted
-
connectBlocking
Same asconnect
but blocks with a timeout until the websocket connected or failed to do so.- Parameters:
timeout
- The connect timeouttimeUnit
- The timeout time unit- Returns:
- Returns whether it succeeded or not.
- Throws:
InterruptedException
- Thrown when the threads get interrupted
-
close
public void close()Initiates the websocket close handshake. This method does not block
In oder to make sure the connection is closed usecloseBlocking
-
closeBlocking
Same asclose
but blocks until the websocket closed or failed to do so.- Throws:
InterruptedException
- Thrown when the threads get interrupted
-
send
Sends text to the connected websocket server. -
send
public void send(byte[] data) Sends binary data to the connected webSocket server. -
getAttachment
public <T> T getAttachment()Description copied from interface:WebSocket
Getter for the connection attachment.- Specified by:
getAttachment
in interfaceWebSocket
- Type Parameters:
T
- The type of the attachment- Returns:
- Returns the user attachment
-
setAttachment
public <T> void setAttachment(T attachment) Description copied from interface:WebSocket
Setter for an attachment on the socket connection. The attachment may be of any type.- Specified by:
setAttachment
in interfaceWebSocket
- Type Parameters:
T
- The type of the attachment- Parameters:
attachment
- The object to be attached to the user
-
getConnections
Description copied from class:AbstractWebSocket
Getter to get all the currently available connections- Specified by:
getConnections
in classAbstractWebSocket
- Returns:
- the currently available connections
-
sendPing
public void sendPing()Description copied from interface:WebSocket
Send a ping to the other end -
run
public void run() -
onSetSSLParameters
Apply specific SSLParameters If you override this method make sure to always call super.onSetSSLParameters() to ensure the hostname validation is active- Parameters:
sslParameters
- the SSLParameters which will be used for the SSLSocket
-
getReadyState
This represents the state of the connection.- Specified by:
getReadyState
in interfaceWebSocket
- Returns:
- Returns '0 = CONNECTING', '1 = OPEN', '2 = CLOSING' or '3 = CLOSED'
-
onWebsocketMessage
Calls subclass' implementation of onMessage.- Specified by:
onWebsocketMessage
in interfaceWebSocketListener
- Parameters:
conn
- The WebSocket instance this event is occurring on.message
- The UTF-8 decoded message that was received.
-
onWebsocketMessage
Description copied from interface:WebSocketListener
Called when an entire binary frame has been received. Do whatever you want here...- Specified by:
onWebsocketMessage
in interfaceWebSocketListener
- Parameters:
conn
- The WebSocket instance this event is occurring on.blob
- The binary message that was received.
-
onWebsocketOpen
Calls subclass' implementation of onOpen.- Specified by:
onWebsocketOpen
in interfaceWebSocketListener
- Parameters:
conn
- The WebSocket instance this event is occurring on.handshake
- The handshake of the websocket instance
-
onWebsocketClose
Calls subclass' implementation of onClose.- Specified by:
onWebsocketClose
in interfaceWebSocketListener
- Parameters:
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.
-
onWebsocketError
Calls subclass' implementation of onIOError.- Specified by:
onWebsocketError
in interfaceWebSocketListener
- Parameters:
conn
- The WebSocket instance this event is occurring on.ex
- The exception that occurred.
Might be null if the exception is not related to any specific connection. For example if the server port could not be bound.
-
onWriteDemand
Description copied from interface:WebSocketListener
This method is used to inform the selector thread that there is data queued to be written to the socket.- Specified by:
onWriteDemand
in interfaceWebSocketListener
- Parameters:
conn
- The WebSocket instance this event is occurring on.
-
onWebsocketCloseInitiated
Description copied from interface:WebSocketListener
send when this peer sends a close handshake- Specified by:
onWebsocketCloseInitiated
in interfaceWebSocketListener
- Parameters:
conn
- The WebSocket instance this event is occurring on.code
- The codes can be looked up here:CloseFrame
reason
- Additional information string
-
onWebsocketClosing
Description copied from interface:WebSocketListener
Called as soon as no further frames are accepted- Specified by:
onWebsocketClosing
in interfaceWebSocketListener
- Parameters:
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.
-
onCloseInitiated
Send when this peer sends a close handshake- Parameters:
code
- The codes can be looked up here:CloseFrame
reason
- Additional information string
-
onClosing
Called as soon as no further frames are accepted- Parameters:
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.
-
getConnection
Getter for the engine- Returns:
- the engine
-
getLocalSocketAddress
- Specified by:
getLocalSocketAddress
in interfaceWebSocketListener
- Parameters:
conn
- The WebSocket instance this event is occurring on.- Returns:
- Returns the address of the endpoint this socket is bound to.
- See Also:
-
getRemoteSocketAddress
- Specified by:
getRemoteSocketAddress
in interfaceWebSocketListener
- Parameters:
conn
- The WebSocket instance this event is occurring on.- Returns:
- Returns the address of the endpoint this socket is connected to, or
null
if it is unconnected. - See Also:
-
onOpen
Called after an opening handshake has been performed and the given websocket is ready to be written on.- Parameters:
handshakedata
- The handshake of the websocket instance
-
onMessage
Callback for string messages received from the remote host- Parameters:
message
- The UTF-8 decoded message that was received.- See Also:
-
onClose
Called after the websocket connection has been closed.- Parameters:
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.
-
onError
Called when errors occurs. If an error causes the websocket connection to failonClose(int, String, boolean)
will be called additionally.
This method will be called primarily because of IO or protocol errors.
If the given exception is an RuntimeException that probably means that you encountered a bug.- Parameters:
ex
- The exception causing this error
-
onMessage
Callback for binary messages received from the remote host- Parameters:
bytes
- The binary message that was received.- See Also:
-
setProxy
Method to set a proxy for this connection- Parameters:
proxy
- the proxy to use for this websocket client
-
setSocket
Deprecated.use setSocketFactoryAccepts bound and unbound sockets.
This method must be called beforeconnect
. 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
Accepts a SocketFactory.
This method must be called beforeconnect
. The socket will be bound to the uri specified in the constructor.- Parameters:
socketFactory
- The socket factory which should be used for the connection.
-
sendFragmentedFrame
Description copied from interface:WebSocket
Allows to send continuous/fragmented frames conveniently.
For more into on this frame type see 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 will received via onMessage instead of onFragmented even though it was send by this method.
- Specified by:
sendFragmentedFrame
in interfaceWebSocket
- Parameters:
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.
-
isOpen
public boolean isOpen()Description copied from interface:WebSocket
Is the websocket in the state OPEN -
isFlushAndClose
public boolean isFlushAndClose()Description copied from interface:WebSocket
Returns true when no further frames may be submitted
This happens before the socket connection is closed.- Specified by:
isFlushAndClose
in interfaceWebSocket
- Returns:
- true when no further frames may be submitted
-
isClosed
public boolean isClosed()Description copied from interface:WebSocket
Is the websocket in the state CLOSED -
isClosing
public boolean isClosing()Description copied from interface:WebSocket
Is the websocket in the state CLOSING -
hasBufferedData
public boolean hasBufferedData()Description copied from interface:WebSocket
Checks if the websocket has buffered data- Specified by:
hasBufferedData
in interfaceWebSocket
- Returns:
- has the websocket buffered data
-
close
public void close(int code) Description copied from interface:WebSocket
sends the closing handshake. may be send in response to an other handshake. -
close
Description copied from interface:WebSocket
sends the closing handshake. may be send in response to an other handshake. -
closeConnection
Description copied from interface:WebSocket
This will close the connection immediately without a proper close handshake. The code and the message therefore won't be transferred over the wire also they will be forwarded to onClose/onWebsocketClose.- Specified by:
closeConnection
in interfaceWebSocket
- Parameters:
code
- the closing codemessage
- the closing message
-
send
Description copied from interface:WebSocket
Send Binary data (plain bytes) to the other end. -
sendFrame
Description copied from interface:WebSocket
Send a frame to the other end -
sendFrame
Description copied from interface:WebSocket
Send a collection of frames to the other end -
getLocalSocketAddress
Description copied from interface:WebSocket
Returns the address of the endpoint this socket is bound to, ornull
if it is not bound.- Specified by:
getLocalSocketAddress
in interfaceWebSocket
- Returns:
- the local socket address or null, if this socket is not bound
-
getRemoteSocketAddress
Description copied from interface:WebSocket
Returns the address of the endpoint this socket is connected to, ornull
if it is unconnected.- Specified by:
getRemoteSocketAddress
in interfaceWebSocket
- Returns:
- the remote socket address or null, if this socket is unconnected
-
getResourceDescriptor
Description copied from interface:WebSocket
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.- Specified by:
getResourceDescriptor
in interfaceWebSocket
- Returns:
- Returns the decoded path component of this URI.
-
hasSSLSupport
public boolean hasSSLSupport()Description copied from interface:WebSocket
Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection- Specified by:
hasSSLSupport
in interfaceWebSocket
- Returns:
- true, if the websocket does use wss and therefore has a SSLSession
-
getSSLSession
Description copied from interface:WebSocket
Returns the ssl session of websocket, if ssl/wss is used for this instance.- Specified by:
getSSLSession
in interfaceWebSocket
- Returns:
- the ssl session of this websocket instance
-
getProtocol
Description copied from interface:WebSocket
Returns the used Sec-WebSocket-Protocol for this websocket connection- Specified by:
getProtocol
in interfaceWebSocket
- Returns:
- the Sec-WebSocket-Protocol or null, if no draft available
-