Top Description Inners Fields Constructors Methods
org.java_websocket.client

public abstract Class WebSocketClient

extends AbstractWebSocket
implements Runnable, WebSocket
Class Inheritance
All Implemented Interfaces
org.java_websocket.WebSocket, java.lang.Runnable
Imports
java.io.IOException, .InputStream, .OutputStream, java.lang.reflect.InvocationTargetException, java.net.InetAddress, .InetSocketAddress, .Proxy, .Socket, .URI, .UnknownHostException, java.nio.ByteBuffer, java.security.KeyManagementException, .NoSuchAlgorithmException, java.util.Collection, .Collections, .Map, .TreeMap, java.util.concurrent.CountDownLatch, .TimeUnit, javax.net.SocketFactory, javax.net.ssl.SSLException, .SSLParameters, .SSLSession, .SSLSocket, .SSLSocketFactory, org.java_websocket.AbstractWebSocket, .WebSocket, .WebSocketImpl, org.java_websocket.drafts.Draft, .Draft_6455, org.java_websocket.enums.Opcode, .ReadyState, org.java_websocket.exceptions.InvalidHandshakeException, org.java_websocket.framing.CloseFrame, .Framedata, org.java_websocket.handshake.HandshakeImpl1Client, .Handshakedata, .ServerHandshake, org.java_websocket.protocols.IProtocol

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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private CountDownLatch
closeLatch

The latch for closeBlocking()

private CountDownLatch
connectLatch

The latch for connectBlocking()

private Thread
connectReadThread

The thread to connect and read message

private int
connectTimeout

The socket timeout value to be used in milliseconds.

private DnsResolver
dnsResolver

DNS resolver that translates a URI to an InetAddress

private Draft
draft

The draft to use

private WebSocketImpl
engine

The underlying engine

private Map<String, String>
headers

The additional headers to use

private OutputStream
ostream

The used OutputStream

private Proxy
proxy

The used proxy, if any

private Socket
socket

The socket for this WebSocketClient

private SocketFactory
socketFactory

The SocketFactory for this WebSocketClient

protected URI
uri

The URI this channel is supposed to connect to.

private Thread
writeThread

The thread to write outgoing message

Inherited from org.java_websocket.AbstractWebSocket:
DEFAULT_READ_BUFFER_SIZE

Constructor Summary

AccessConstructor and Description
public
WebSocketClient(URI
the server URI to connect to
serverUri
)

Constructs a WebSocketClient instance and sets it to the connect to the specified URI.

public
WebSocketClient(URI
the server URI to connect to
serverUri
,
Draft
The draft which should be used for this connection
protocolDraft
)

Constructs a WebSocketClient instance and sets it to the connect to the specified URI.

public
WebSocketClient(URI
the server URI to connect to
serverUri
,
Map<String, String>
Additional HTTP-Headers
httpHeaders
)

Constructs a WebSocketClient instance and sets it to the connect to the specified URI.

public
WebSocketClient(URI
the server URI to connect to
serverUri
,
Draft
The draft which should be used for this connection
protocolDraft
,
Map<String, String>
Additional HTTP-Headers
httpHeaders
)

Constructs a WebSocketClient instance and sets it to the connect to the specified URI.

public
WebSocketClient(URI
the server URI to connect to
serverUri
,
Draft
The draft which should be used for this connection
protocolDraft
,
Map<String, String>
Additional HTTP-Headers
httpHeaders
,
int
The Timeout for the connection
connectTimeout
)

Constructs a WebSocketClient instance and sets it to the connect to the specified URI.

Method Summary

Modifier and TypeMethod and Description
public void
addHeader(String
Name of the header to add.
key
,
String
Value of the header to add.
value
)

public void
public void
close()

Implements org.java_websocket.WebSocket.close.

Initiates the websocket close handshake.

public void
close(int code)

Implements org.java_websocket.WebSocket.close.

sends the closing handshake.

public void
close(int code, String message)

Implements org.java_websocket.WebSocket.close.

sends the closing handshake.

public void
closeBlocking()

Same as close but blocks until the websocket closed or failed to do so.

public void
closeConnection(int code, String message)

Implements org.java_websocket.WebSocket.closeConnection.

This will close the connection immediately without a proper close handshake.

public void
connect()

Initiates the websocket connection.

public boolean

Returns:

Returns whether it succeeded or not.
connectBlocking
()

Same as connect but blocks until the websocket connected or failed to do so.

public boolean

Returns:

Returns whether it succeeded or not.
connectBlocking
(long
The connect timeout
timeout
,
TimeUnit
The timeout time unit
timeUnit
)

Same as connect but blocks with a timeout until the websocket connected or failed to do so.

public <T> T
getAttachment()

Implements org.java_websocket.WebSocket.getAttachment.

Getter for the connection attachment.

public WebSocket

Returns:

the engine
getConnection
()

Getter for the engine

protected Collection<WebSocket>
getConnections()

Implements abstract org.java_websocket.AbstractWebSocket.getConnections.

Getter to get all the currently available connections

public Draft

Returns:

The draft used for this client
getDraft
()

Implements org.java_websocket.WebSocket.getDraft.

Returns the protocol version this channel uses.

public InetSocketAddress
public InetSocketAddress
getLocalSocketAddress()

Implements org.java_websocket.WebSocket.getLocalSocketAddress.

Returns the address of the endpoint this socket is bound to, or null if it is not bound.

private int

Returns:

the specified port or the default port for the specific scheme
getPort
()

Extract the specified port

public IProtocol
getProtocol()

Implements org.java_websocket.WebSocket.getProtocol.

Returns the used Sec-WebSocket-Protocol for this websocket connection

public ReadyState
getReadyState()

Implements org.java_websocket.WebSocket.getReadyState.

This represents the state of the connection.

public InetSocketAddress
public InetSocketAddress
getRemoteSocketAddress()

Implements org.java_websocket.WebSocket.getRemoteSocketAddress.

Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

public String
getResourceDescriptor()

Implements org.java_websocket.WebSocket.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.

public Socket

Returns:

the socket used for this connection
getSocket
()

Returns the socket to allow Hostname Verification

public SSLSession
getSSLSession()

Implements org.java_websocket.WebSocket.getSSLSession.

Returns the ssl session of websocket, if ssl/wss is used for this instance.

public URI

Returns:

the URI connected to
getURI
()

Returns the URI that this WebSocketClient is connected to.

private void
handleIOException(IOException
the IOException causing a eot.
e
)

Method to give some additional info for specific IOExceptions

public boolean
hasBufferedData()

Implements org.java_websocket.WebSocket.hasBufferedData.

Checks if the websocket has buffered data

public boolean
hasSSLSupport()

Implements org.java_websocket.WebSocket.hasSSLSupport.

Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection

public boolean
isClosed()

Implements org.java_websocket.WebSocket.isClosed.

Is the websocket in the state CLOSED

public boolean
isClosing()

Implements org.java_websocket.WebSocket.isClosing.

Is the websocket in the state CLOSING

public boolean
isFlushAndClose()

Implements org.java_websocket.WebSocket.isFlushAndClose.

Returns true when no further frames may be submitted This happens before the socket connection is closed.

public boolean
isOpen()

Implements org.java_websocket.WebSocket.isOpen.

Is the websocket in the state OPEN

public abstract void
onClose(int
The codes can be looked up here: CloseFrame
code
,
String
Additional information string
reason
,
boolean
Returns whether or not the closing of the connection was initiated by the remote host.
remote
)

Called after the websocket connection has been closed.

public void
onCloseInitiated(int
The codes can be looked up here: CloseFrame
code
,
String
Additional information string
reason
)

Send when this peer sends a close handshake

public void
onClosing(int
The codes can be looked up here: CloseFrame
code
,
String
Additional information string
reason
,
boolean
Returns whether or not the closing of the connection was initiated by the remote host.
remote
)

Called as soon as no further frames are accepted

public abstract void
onError(Exception
The exception causing this error
ex
)

Called when errors occurs.

public abstract void
onMessage(String
The UTF-8 decoded message that was received.
message
)

Callback for string messages received from the remote host

public void
onMessage(ByteBuffer
The binary message that was received.
bytes
)

Callback for binary messages received from the remote host

public abstract void
onOpen(ServerHandshake
The handshake of the websocket instance
handshakedata
)

Called after an opening handshake has been performed and the given websocket is ready to be written on.

protected void
onSetSSLParameters(SSLParameters
the SSLParameters which will be used for the SSLSocket
sslParameters
)

Apply specific SSLParameters If you override this method make sure to always call super.onSetSSLParameters() to ensure the hostname validation is active

public final void
onWebsocketClose(WebSocket conn, int code, String reason, boolean remote)

Implements org.java_websocket.WebSocketListener.onWebsocketClose.

Calls subclass' implementation of onClose.

public void
onWebsocketCloseInitiated(WebSocket conn, int code, String reason)

Implements org.java_websocket.WebSocketListener.onWebsocketCloseInitiated.

send when this peer sends a close handshake

public void
onWebsocketClosing(WebSocket conn, int code, String reason, boolean remote)

Implements org.java_websocket.WebSocketListener.onWebsocketClosing.

Called as soon as no further frames are accepted

public final void
onWebsocketError(WebSocket conn, Exception ex)

Implements org.java_websocket.WebSocketListener.onWebsocketError.

Calls subclass' implementation of onIOError.

public final void
onWebsocketMessage(WebSocket conn, String message)

Implements org.java_websocket.WebSocketListener.onWebsocketMessage.

Calls subclass' implementation of onMessage.

public final void
onWebsocketMessage(WebSocket conn, ByteBuffer blob)

Implements org.java_websocket.WebSocketListener.onWebsocketMessage.

Called when an entire binary frame has been received.

public final void
onWebsocketOpen(WebSocket conn, Handshakedata handshake)

Implements org.java_websocket.WebSocketListener.onWebsocketOpen.

Calls subclass' implementation of onOpen.

public final void
onWriteDemand(WebSocket conn)

Implements org.java_websocket.WebSocketListener.onWriteDemand.

This method is used to inform the selector thread that there is data queued to be written to the socket.

private boolean
public void
reconnect()

Reinitiates the websocket connection.

public boolean

Returns:

Returns whether it succeeded or not.
reconnectBlocking
()

Same as reconnect but blocks until the websocket reconnected or failed to do so.

public String

Returns:

the previous value associated with key, or null if there was no mapping for key.
removeHeader
(String
Name of the header to remove.
key
)

private void
reset()

Reset everything relevant to allow a reconnect

public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

public void
send(String
The string which will be transmitted.
text
)

Implements org.java_websocket.WebSocket.send.

Sends text to the connected websocket server.

public void
send(byte[]
The byte-Array of data to send to the WebSocket server.
data
)

Implements org.java_websocket.WebSocket.send.

Sends binary data to the connected webSocket server.

public void
send(ByteBuffer bytes)

Implements org.java_websocket.WebSocket.send.

Send Binary data (plain bytes) to the other end.

public void
sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin)

Implements org.java_websocket.WebSocket.sendFragmentedFrame.

Allows to send continuous/fragmented frames conveniently.

public void
sendFrame(Framedata framedata)

Implements org.java_websocket.WebSocket.sendFrame.

Send a frame to the other end

public void
sendFrame(Collection<Framedata> frames)

Implements org.java_websocket.WebSocket.sendFrame.

Send a collection of frames to the other end

private void
sendHandshake()

Create and send the handshake to the other endpoint

public void
sendPing()

Implements org.java_websocket.WebSocket.sendPing.

Send a ping to the other end

public <T> void
setAttachment(T attachment)

Implements org.java_websocket.WebSocket.setAttachment.

Setter for an attachment on the socket connection.

public void
setDnsResolver(DnsResolver
The DnsResolver to use.
dnsResolver
)

Sets a custom DNS resolver.

public void
setProxy(Proxy
the proxy to use for this websocket client
proxy
)

Method to set a proxy for this connection

public void
setSocket(Socket
The socket which should be used for the connection
socket
)
Deprecated use setSocketFactory

Accepts bound and unbound sockets.

public void
setSocketFactory(SocketFactory
The socket factory which should be used for the connection.
socketFactory
)

Accepts a SocketFactory.

private void
Inherited from org.java_websocket.AbstractWebSocket:
getConnectionLostTimeoutgetReceiveBufferSizeisDaemonisReuseAddrisTcpNoDelaysetConnectionLostTimeoutsetDaemonsetReceiveBufferSizesetReuseAddrsetTcpNoDelaystartConnectionLostTimerstopConnectionLostTimer