public abstract class WebSocketClient extends AbstractWebSocket implements java.lang.Runnable, WebSocket
connect(), then receive events like onMessage(String) via the overloaded methods and to send(String) data to the server.WebSocket.READYSTATE, WebSocket.RoleDEFAULT_PORT, DEFAULT_WSS_PORT| 构造器和说明 |
|---|
WebSocketClient(java.net.URI serverUri)
Constructs a WebSocketClient instance and sets it to the connect to the
specified URI.
|
WebSocketClient(java.net.URI serverUri,
Draft protocolDraft)
Constructs a WebSocketClient instance and sets it to the connect to the
specified URI.
|
WebSocketClient(java.net.URI serverUri,
Draft protocolDraft,
java.util.Map<java.lang.String,java.lang.String> httpHeaders,
int connectTimeout)
Constructs a WebSocketClient instance and sets it to the connect to the
specified URI.
|
| 限定符和类型 | 方法和说明 |
|---|---|
void |
close()
Initiates the websocket close handshake.
|
void |
close(int code)
sends the closing handshake.
|
void |
close(int code,
java.lang.String message)
sends the closing handshake.
|
void |
closeBlocking()
Same as
close but blocks until the websocket closed or failed to do so. |
void |
closeConnection(int code,
java.lang.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. |
WebSocket |
getConnection()
Getter for the engine
|
Draft |
getDraft()
Returns the protocol version this channel uses.
|
java.net.InetSocketAddress |
getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to.
|
java.net.InetSocketAddress |
getLocalSocketAddress(WebSocket conn) |
WebSocket.READYSTATE |
getReadyState()
This represents the state of the connection.
|
java.net.InetSocketAddress |
getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or
null if it is unconnected. |
java.net.InetSocketAddress |
getRemoteSocketAddress(WebSocket conn) |
java.lang.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. |
java.net.Socket |
getSocket()
Returns the socket to allow Hostname Verification
|
java.net.URI |
getURI()
Returns the URI that this WebSocketClient is connected to.
|
boolean |
hasBufferedData()
Checks if the websocket has buffered data
|
boolean |
isClosed()
Is the websocket in the state CLOSED
|
boolean |
isClosing()
Is the websocket in the state CLOSING
|
boolean |
isConnecting()
Is the websocket in the state CONNECTING
|
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,
java.lang.String reason,
boolean remote) |
void |
onCloseInitiated(int code,
java.lang.String reason)
Send when this peer sends a close handshake
|
void |
onClosing(int code,
java.lang.String reason,
boolean remote)
Called as soon as no further frames are accepted
|
abstract void |
onError(java.lang.Exception ex) |
void |
onFragment(Framedata frame) |
void |
onMessage(java.nio.ByteBuffer bytes) |
abstract void |
onMessage(java.lang.String message) |
abstract void |
onOpen(ServerHandshake handshakedata) |
void |
onWebsocketClose(WebSocket conn,
int code,
java.lang.String reason,
boolean remote)
Calls subclass' implementation of onClose.
|
void |
onWebsocketCloseInitiated(WebSocket conn,
int code,
java.lang.String reason)
send when this peer sends a close handshake
|
void |
onWebsocketClosing(WebSocket conn,
int code,
java.lang.String reason,
boolean remote)
Called as soon as no further frames are accepted
|
void |
onWebsocketError(WebSocket conn,
java.lang.Exception ex)
Calls subclass' implementation of onIOError.
|
void |
onWebsocketMessage(WebSocket conn,
java.nio.ByteBuffer blob)
Called when an entire binary frame has been received.
|
void |
onWebsocketMessage(WebSocket conn,
java.lang.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(java.nio.ByteBuffer bytes)
Send Binary data (plain bytes) to the other end.
|
void |
send(java.lang.String text)
Sends text to the connected websocket server.
|
void |
sendFragmentedFrame(Framedata.Opcode op,
java.nio.ByteBuffer buffer,
boolean fin)
Allows to send continuous/fragmented frames conveniently.
|
void |
sendFrame(Framedata framedata)
Send a frame to the other end
|
void |
sendPing()
Send a ping to the other end
|
void |
setProxy(java.net.Proxy proxy) |
void |
setSocket(java.net.Socket socket)
Accepts bound and unbound sockets.
|
getConnectionLostTimeout, isTcpNoDelay, setConnectionLostTimeout, setTcpNoDelaypublic WebSocketClient(java.net.URI serverUri)
serverUri - the server URI to connect topublic WebSocketClient(java.net.URI serverUri,
Draft protocolDraft)
serverUri - the server URI to connect toprotocolDraft - The draft which should be used for this connectionpublic WebSocketClient(java.net.URI serverUri,
Draft protocolDraft,
java.util.Map<java.lang.String,java.lang.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 java.net.URI getURI()
public Draft getDraft()
public java.net.Socket getSocket()
public void connect()
public boolean connectBlocking()
throws java.lang.InterruptedException
connect but blocks until the websocket connected or failed to do so.java.lang.InterruptedException - Thrown when the threads get interruptedpublic void close()
closeBlockingpublic void closeBlocking()
throws java.lang.InterruptedException
close but blocks until the websocket closed or failed to do so.java.lang.InterruptedException - Thrown when the threads get interruptedpublic void send(java.lang.String text)
throws java.nio.channels.NotYetConnectedException
public void send(byte[] data)
throws java.nio.channels.NotYetConnectedException
public void sendPing()
throws java.nio.channels.NotYetConnectedException
WebSocketpublic void run()
run 在接口中 java.lang.Runnablepublic WebSocket.READYSTATE getReadyState()
getReadyState 在接口中 WebSocketpublic final void onWebsocketMessage(WebSocket conn, java.lang.String message)
onWebsocketMessage 在接口中 WebSocketListenerconn - The WebSocket instance this event is occurring on.message - The UTF-8 decoded message that was received.public final void onWebsocketMessage(WebSocket conn, java.nio.ByteBuffer blob)
WebSocketListeneronWebsocketMessage 在接口中 WebSocketListenerconn - The WebSocket instance this event is occurring on.blob - The binary message that was received.public void onWebsocketMessageFragment(WebSocket conn, Framedata frame)
WebSocketAdapteronWebsocketMessageFragment 在接口中 WebSocketListeneronWebsocketMessageFragment 在类中 WebSocketAdapterconn - The WebSocket instance this event is occurring on.frame - The fragmented frameWebSocketListener.onWebsocketMessageFragment(WebSocket, Framedata)public final void onWebsocketOpen(WebSocket conn, Handshakedata handshake)
onWebsocketOpen 在接口中 WebSocketListenerconn - The WebSocket instance this event is occuring on.handshake - The handshake of the websocket instancepublic final void onWebsocketClose(WebSocket conn, int code, java.lang.String reason, boolean remote)
onWebsocketClose 在接口中 WebSocketListenerconn - The WebSocket instance this event is occuring on.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 remote host.public final void onWebsocketError(WebSocket conn, java.lang.Exception ex)
onWebsocketError 在接口中 WebSocketListenerconn - The WebSocket instance this event is occuring on.ex - The exception that occurred. public final void onWriteDemand(WebSocket conn)
WebSocketListeneronWriteDemand 在接口中 WebSocketListenerconn - The WebSocket instance this event is occuring on.public void onWebsocketCloseInitiated(WebSocket conn, int code, java.lang.String reason)
WebSocketListeneronWebsocketCloseInitiated 在接口中 WebSocketListenerconn - The WebSocket instance this event is occuring on.code - The codes can be looked up here: CloseFramereason - Additional information stringpublic void onWebsocketClosing(WebSocket conn, int code, java.lang.String reason, boolean remote)
WebSocketListeneronWebsocketClosing 在接口中 WebSocketListenerconn - The WebSocket instance this event is occuring on.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 remote host.public void onCloseInitiated(int code,
java.lang.String reason)
code - The codes can be looked up here: CloseFramereason - Additional information stringpublic void onClosing(int code,
java.lang.String reason,
boolean remote)
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 remote host.public WebSocket getConnection()
public java.net.InetSocketAddress getLocalSocketAddress(WebSocket conn)
getLocalSocketAddress 在接口中 WebSocketListenerconn - The WebSocket instance this event is occuring on.WebSocket.getLocalSocketAddress()public java.net.InetSocketAddress getRemoteSocketAddress(WebSocket conn)
getRemoteSocketAddress 在接口中 WebSocketListenerconn - The WebSocket instance this event is occuring on.null if it is unconnected.WebSocket.getRemoteSocketAddress()public abstract void onOpen(ServerHandshake handshakedata)
public abstract void onMessage(java.lang.String message)
public abstract void onClose(int code,
java.lang.String reason,
boolean remote)
public abstract void onError(java.lang.Exception ex)
public void onMessage(java.nio.ByteBuffer bytes)
public void onFragment(Framedata frame)
public void setProxy(java.net.Proxy proxy)
public void setSocket(java.net.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 sendFragmentedFrame(Framedata.Opcode op, java.nio.ByteBuffer buffer, boolean fin)
WebSocketsendFragmentedFrame 在接口中 WebSocketop - 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()
WebSocketpublic boolean isFlushAndClose()
WebSocketisFlushAndClose 在接口中 WebSocketpublic boolean isClosed()
WebSocketpublic boolean isClosing()
WebSocketpublic boolean isConnecting()
WebSocketisConnecting 在接口中 WebSocketpublic boolean hasBufferedData()
WebSockethasBufferedData 在接口中 WebSocketpublic void close(int code)
WebSocketpublic void close(int code,
java.lang.String message)
WebSocketpublic void closeConnection(int code,
java.lang.String message)
WebSocketcloseConnection 在接口中 WebSocketcode - the closing codemessage - the closing messagepublic void send(java.nio.ByteBuffer bytes)
throws java.lang.IllegalArgumentException,
java.nio.channels.NotYetConnectedException
WebSocketpublic void sendFrame(Framedata framedata)
WebSocketpublic java.net.InetSocketAddress getLocalSocketAddress()
WebSocketgetLocalSocketAddress 在接口中 WebSocketpublic java.net.InetSocketAddress getRemoteSocketAddress()
WebSocketnull if it is unconnected.getRemoteSocketAddress 在接口中 WebSocketpublic java.lang.String getResourceDescriptor()
WebSocketgetResourceDescriptor 在接口中 WebSocket