public class TyrusWebSocket extends Object
Instance of this class represents one bi-directional websocket connection.
Constructor and Description |
---|
TyrusWebSocket(ProtocolHandler protocolHandler,
TyrusEndpointWrapper endpointWrapper)
Create new instance, set
ProtocolHandler and register TyrusEndpointWrapper . |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this
TyrusWebSocket . |
void |
close(CloseReason closeReason)
Closes this
TyrusWebSocket using the CloseReason . |
void |
close(int code,
String reason)
Closes this
TyrusWebSocket using the specified status code and
reason. |
boolean |
isConnected()
Convenience method to determine if this
TyrusWebSocket instance is connected. |
void |
onClose(CloseFrame frame)
This callback will be invoked when the remote endpoint sent a closing frame.
|
void |
onConnect(UpgradeRequest upgradeRequest,
String subProtocol,
List<Extension> extensions,
String connectionId,
DebugContext debugContext)
This callback will be invoked when the opening handshake between both
endpoints has been completed.
|
void |
onFragment(BinaryFrame frame,
boolean last)
This callback will be invoked when a fragmented binary message has
been received.
|
void |
onFragment(TextFrame frame,
boolean last)
This callback will be invoked when a fragmented textual message has
been received.
|
void |
onMessage(BinaryFrame frame)
This callback will be invoked when a binary message has been received.
|
void |
onMessage(TextFrame frame)
This callback will be invoked when a text message has been received.
|
void |
onPing(PingFrame frame)
This callback will be invoked when the remote endpoint has sent a ping frame.
|
void |
onPong(PongFrame frame)
This callback will be invoked when the remote endpoint has sent a pong
frame.
|
Future<Frame> |
sendBinary(byte[] data)
Send a binary frame to the remote endpoint.
|
Future<Frame> |
sendBinary(byte[] bytes,
boolean last)
Sends a fragment of a complete message.
|
Future<Frame> |
sendBinary(byte[] bytes,
int off,
int len,
boolean last)
Sends a fragment of a complete message.
|
void |
sendBinary(byte[] data,
SendHandler handler)
Send a binary frame to the remote endpoint.
|
Future<Frame> |
sendPing(byte[] data)
Sends a
ping frame with the specified payload (if any). |
Future<Frame> |
sendPong(byte[] data)
Sends a
ping frame with the specified payload (if any). |
Future<Frame> |
sendRawFrame(ByteBuffer data)
Send a frame to the remote endpoint.
|
Future<Frame> |
sendText(String data)
Send a text frame to the remote endpoint.
|
Future<Frame> |
sendText(String fragment,
boolean last)
Sends a fragment of a complete message.
|
void |
sendText(String data,
SendHandler handler)
Send a text frame to the remote endpoint.
|
void |
setWriteTimeout(long timeoutMs)
Sets the timeout for the writing operation.
|
public TyrusWebSocket(ProtocolHandler protocolHandler, TyrusEndpointWrapper endpointWrapper)
ProtocolHandler
and register TyrusEndpointWrapper
.protocolHandler
- used for writing data (sending).endpointWrapper
- notifies registered endpoints about incoming events.public void setWriteTimeout(long timeoutMs)
timeoutMs
- timeout in milliseconds.public boolean isConnected()
TyrusWebSocket
instance is connected.true
if the TyrusWebSocket
is connected, false
otherwise.public void onClose(CloseFrame frame)
The execution of this method is synchronized using ProtocolHandler
instance; see TYRUS-385. Prevents
multiple invocations, especially from container/user code.
frame
- the close frame from the remote endpoint.public void onConnect(UpgradeRequest upgradeRequest, String subProtocol, List<Extension> extensions, String connectionId, DebugContext debugContext)
upgradeRequest
- request associated with this socket.subProtocol
- negotiated subprotocol.extensions
- negotiated extensions.connectionId
- connection id.debugContext
- debug context.public void onFragment(BinaryFrame frame, boolean last)
frame
- the binary data received from the remote endpoint.last
- flag indicating whether or not the payload received is the final fragment of a message.public void onFragment(TextFrame frame, boolean last)
frame
- the text received from the remote endpoint.last
- flag indicating whether or not the payload received is the final fragment of a message.public void onMessage(BinaryFrame frame)
frame
- the binary data received from the remote endpoint.public void onMessage(TextFrame frame)
frame
- the text received from the remote endpoint.public void onPing(PingFrame frame)
frame
- the ping frame from the remote endpoint.public void onPong(PongFrame frame)
frame
- the pong frame from the remote endpoint.public void close()
TyrusWebSocket
.public void close(int code, String reason)
TyrusWebSocket
using the specified status code and
reason.code
- the closing status code.reason
- the reason, if any.public void close(CloseReason closeReason)
TyrusWebSocket
using the CloseReason
.closeReason
- the close reason.public Future<Frame> sendBinary(byte[] data)
data
- data to be sent.Future
which could be used to control/check the sending completion state.public void sendBinary(byte[] data, SendHandler handler)
data
- data to be sent.handler
- SendHandler.onResult(javax.websocket.SendResult)
will be called when sending is complete.public Future<Frame> sendText(String data)
data
- data to be sent.Future
which could be used to control/check the sending completion state.public void sendText(String data, SendHandler handler)
data
- data to be sent.handler
- SendHandler.onResult(javax.websocket.SendResult)
will be called when sending is complete.public Future<Frame> sendRawFrame(ByteBuffer data)
data
- complete data frame.Future
which could be used to control/check the sending completion state.public Future<Frame> sendPing(byte[] data)
ping
frame with the specified payload (if any).data
- optional payload. Note that payload length is restricted to 125 bytes or less.Future
which could be used to control/check the sending completion state.public Future<Frame> sendPong(byte[] data)
ping
frame with the specified payload (if any).
It may seem odd to send a pong frame, however, RFC-6455 states: "A Pong frame MAY be sent unsolicited. This serves as a unidirectional heartbeat. A response to an unsolicited Pong frame is not expected."
data
- optional payload. Note that payload length is restricted
to 125 bytes or less.Future
which could be used to control/check the sending completion state.public Future<Frame> sendText(String fragment, boolean last)
fragment
- the textual fragment to send.last
- boolean indicating if this message fragment is the last.Future
which could be used to control/check the sending completion state.public Future<Frame> sendBinary(byte[] bytes, boolean last)
bytes
- the binary fragment to send.last
- boolean indicating if this message fragment is the last.Future
which could be used to control/check the sending completion state.public Future<Frame> sendBinary(byte[] bytes, int off, int len, boolean last)
bytes
- the binary fragment to send.off
- the offset within the fragment to send.len
- the number of bytes of the fragment to send.last
- boolean indicating if this message fragment is the last.Future
which could be used to control/check the sending completion state.Copyright © 2012–2019 Oracle Corporation. All rights reserved.