public abstract class WebSocketClient extends WebSocketAdapter implements Runnable, WebSocket
connect()
, then receive events like onMessage(String)
via the overloaded methods and to send(String)
data to the server.WebSocket.READYSTATE, WebSocket.Role
Modifier and Type | Field and Description |
---|---|
protected URI |
uri
The URI this channel is supposed to connect to.
|
DEFAULT_PORT, DEFAULT_WSS_PORT
Constructor and Description |
---|
WebSocketClient(URI serverURI)
This open a websocket connection as specified by rfc6455
|
WebSocketClient(URI serverUri,
Draft draft)
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) |
Modifier and Type | Method and Description |
---|---|
void |
close()
Initiates the websocket close handshake.
|
void |
close(int code) |
void |
close(int code,
String message)
sends the closing handshake.
|
void |
closeBlocking() |
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.Returns whether it succeeded or not. |
WebSocket |
getConnection() |
Draft |
getDraft()
Returns the protocol version this channel uses.
For more infos see https://github.com/TooTallNate/Java-WebSocket/wiki/Drafts |
InetSocketAddress |
getLocalSocketAddress() |
InetSocketAddress |
getLocalSocketAddress(WebSocket conn) |
WebSocket.READYSTATE |
getReadyState()
This represents the state of the connection.
|
InetSocketAddress |
getRemoteSocketAddress() |
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. |
URI |
getURI()
Returns the URI that this WebSocketClient is connected to.
|
boolean |
hasBufferedData() |
boolean |
isClosed()
Returns whether the close handshake has been completed and the socket is closed.
|
boolean |
isClosing() |
boolean |
isConnecting() |
boolean |
isFlushAndClose()
Returns true when no further frames may be submitted
This happens before the socket connection is closed. |
boolean |
isOpen() |
abstract void |
onClose(int code,
String reason,
boolean remote) |
void |
onCloseInitiated(int code,
String reason) |
void |
onClosing(int code,
String reason,
boolean remote) |
abstract void |
onError(Exception ex) |
void |
onFragment(Framedata frame) |
void |
onMessage(ByteBuffer bytes) |
abstract void |
onMessage(String message) |
abstract void |
onOpen(ServerHandshake handshakedata) |
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 |
onWebsocketMessageFragment(WebSocket conn,
Framedata frame)
This default implementation does not do anything.
|
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 |
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(Framedata.Opcode op,
ByteBuffer buffer,
boolean fin)
Allows to send continuous/fragmented frames conveniently.
|
void |
sendFrame(Framedata framedata) |
void |
setProxy(Proxy proxy) |
void |
setSocket(Socket socket)
Accepts bound and unbound sockets.
This method must be called before connect . |
getFlashPolicy, onWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeReceivedAsServer, onWebsocketHandshakeSentAsClient, onWebsocketPing, onWebsocketPong
protected URI uri
public WebSocketClient(URI serverURI)
public WebSocketClient(URI serverUri, Draft draft)
public URI getURI()
public Draft getDraft()
public void connect()
public boolean connectBlocking() throws InterruptedException
connect
but blocks until the websocket connected or failed to do so.InterruptedException
public void close()
closeBlocking
public void closeBlocking() throws InterruptedException
InterruptedException
public void send(String text) throws NotYetConnectedException
send
in interface WebSocket
text
- The string which will be transmitted.NotYetConnectedException
public void send(byte[] data) throws NotYetConnectedException
send
in interface WebSocket
data
- The byte-Array of data to send to the WebSocket server.NotYetConnectedException
public WebSocket.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 void onWebsocketMessageFragment(WebSocket conn, Framedata frame)
WebSocketAdapter
onWebsocketMessageFragment
in interface WebSocketListener
onWebsocketMessageFragment
in class WebSocketAdapter
WebSocketListener.onWebsocketMessageFragment(WebSocket, Framedata)
public final void onWebsocketOpen(WebSocket conn, Handshakedata handshake)
onWebsocketOpen
in interface WebSocketListener
conn
- The WebSocket instance this event is occuring on.public final void onWebsocketClose(WebSocket conn, int code, String reason, boolean remote)
onWebsocketClose
in interface WebSocketListener
conn
- The WebSocket instance this event is occuring on.public final void onWebsocketError(WebSocket conn, Exception ex)
onWebsocketError
in interface WebSocketListener
ex
- The exception that occurred. public final void onWriteDemand(WebSocket conn)
WebSocketListener
onWriteDemand
in interface WebSocketListener
public void onWebsocketCloseInitiated(WebSocket conn, int code, String reason)
WebSocketListener
onWebsocketCloseInitiated
in interface WebSocketListener
public void onWebsocketClosing(WebSocket conn, int code, String reason, boolean remote)
WebSocketListener
onWebsocketClosing
in interface WebSocketListener
public void onCloseInitiated(int code, String reason)
public void onClosing(int code, String reason, boolean remote)
public WebSocket getConnection()
public InetSocketAddress getLocalSocketAddress(WebSocket conn)
getLocalSocketAddress
in interface WebSocketListener
public InetSocketAddress getRemoteSocketAddress(WebSocket conn)
getRemoteSocketAddress
in interface WebSocketListener
public abstract void onOpen(ServerHandshake handshakedata)
public abstract void onMessage(String message)
public abstract void onClose(int code, String reason, boolean remote)
public abstract void onError(Exception ex)
public void onMessage(ByteBuffer bytes)
public void onFragment(Framedata frame)
public void setProxy(Proxy proxy)
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.public void sendFragmentedFrame(Framedata.Opcode op, ByteBuffer buffer, boolean fin)
WebSocket
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 isFlushAndClose()
WebSocket
isFlushAndClose
in interface WebSocket
public boolean isClosed()
WebSocket
public boolean isConnecting()
isConnecting
in interface WebSocket
public boolean hasBufferedData()
hasBufferedData
in interface WebSocket
public void close(int code, String message)
WebSocket
public void closeConnection(int code, String message)
WebSocket
closeConnection
in interface WebSocket
public void send(ByteBuffer bytes) throws IllegalArgumentException, NotYetConnectedException
WebSocket
send
in interface WebSocket
IllegalArgumentException
NotYetConnectedException
public InetSocketAddress getLocalSocketAddress()
getLocalSocketAddress
in interface WebSocket
public InetSocketAddress getRemoteSocketAddress()
getRemoteSocketAddress
in interface WebSocket
public String getResourceDescriptor()
WebSocket
getResourceDescriptor
in interface WebSocket
Copyright © 2016. All rights reserved.