Package org.glassfish.tyrus.core
Class TyrusWebSocket
- java.lang.Object
-
- org.glassfish.tyrus.core.TyrusWebSocket
-
public class TyrusWebSocket extends java.lang.Object
Tyrus representation of web socket connection.Instance of this class represents one bi-directional websocket connection.
-
-
Constructor Summary
Constructors Constructor Description TyrusWebSocket(ProtocolHandler protocolHandler, TyrusEndpointWrapper endpointWrapper)
Create new instance, setProtocolHandler
and registerTyrusEndpointWrapper
.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
close()
Closes thisTyrusWebSocket
.void
close(int code, java.lang.String reason)
Closes thisTyrusWebSocket
using the specified status code and reason.void
close(CloseReason closeReason)
Closes thisTyrusWebSocket
using theCloseReason
.boolean
isConnected()
Convenience method to determine if thisTyrusWebSocket
instance is connected.void
onClose(CloseFrame frame)
This callback will be invoked when the remote endpoint sent a closing frame.void
onConnect(UpgradeRequest upgradeRequest, java.lang.String subProtocol, java.util.List<Extension> extensions, java.lang.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.java.util.concurrent.Future<Frame>
sendBinary(byte[] data)
Deprecated.java.util.concurrent.Future<Frame>
sendBinary(byte[] bytes, boolean last)
Deprecated.java.util.concurrent.Future<Frame>
sendBinary(byte[] bytes, boolean last, WriterInfo writerInfo)
Sends a fragment of a complete message.java.util.concurrent.Future<Frame>
sendBinary(byte[] bytes, int off, int len, boolean last)
Deprecated.java.util.concurrent.Future<Frame>
sendBinary(byte[] bytes, int off, int len, boolean last, WriterInfo writerInfo)
Sends a fragment of a complete message.void
sendBinary(byte[] data, SendHandler handler)
Deprecated.void
sendBinary(byte[] data, SendHandler handler, WriterInfo writerInfo)
Send a binary frame to the remote endpoint.java.util.concurrent.Future<Frame>
sendBinary(byte[] data, WriterInfo writerInfo)
Send a binary frame to the remote endpoint.java.util.concurrent.Future<Frame>
sendPing(byte[] data)
Sends aping
frame with the specified payload (if any).java.util.concurrent.Future<Frame>
sendPong(byte[] data)
Sends aping
frame with the specified payload (if any).java.util.concurrent.Future<Frame>
sendRawFrame(java.nio.ByteBuffer data)
Send a frame to the remote endpoint.java.util.concurrent.Future<Frame>
sendText(java.lang.String data)
Deprecated.java.util.concurrent.Future<Frame>
sendText(java.lang.String fragment, boolean last)
Deprecated.java.util.concurrent.Future<Frame>
sendText(java.lang.String fragment, boolean last, WriterInfo writerInfo)
Sends a fragment of a complete message.void
sendText(java.lang.String data, SendHandler handler)
Deprecated.void
sendText(java.lang.String data, SendHandler handler, WriterInfo writerInfo)
Send a text frame to the remote endpoint.java.util.concurrent.Future<Frame>
sendText(java.lang.String data, WriterInfo writerInfo)
Send a text frame to the remote endpoint.void
setWriteTimeout(long timeoutMs)
Sets the timeout for the writing operation.
-
-
-
Constructor Detail
-
TyrusWebSocket
public TyrusWebSocket(ProtocolHandler protocolHandler, TyrusEndpointWrapper endpointWrapper)
Create new instance, setProtocolHandler
and registerTyrusEndpointWrapper
.- Parameters:
protocolHandler
- used for writing data (sending).endpointWrapper
- notifies registered endpoints about incoming events.
-
-
Method Detail
-
setWriteTimeout
public void setWriteTimeout(long timeoutMs)
Sets the timeout for the writing operation.- Parameters:
timeoutMs
- timeout in milliseconds.
-
isConnected
public boolean isConnected()
Convenience method to determine if thisTyrusWebSocket
instance is connected.- Returns:
true
if theTyrusWebSocket
is connected,false
otherwise.
-
onClose
public void onClose(CloseFrame frame)
This callback will be invoked when the remote endpoint sent a closing frame.The execution of this method is synchronized using
ProtocolHandler
instance; see TYRUS-385. Prevents multiple invocations, especially from container/user code.- Parameters:
frame
- the close frame from the remote endpoint.
-
onConnect
public void onConnect(UpgradeRequest upgradeRequest, java.lang.String subProtocol, java.util.List<Extension> extensions, java.lang.String connectionId, DebugContext debugContext)
This callback will be invoked when the opening handshake between both endpoints has been completed.- Parameters:
upgradeRequest
- request associated with this socket.subProtocol
- negotiated subprotocol.extensions
- negotiated extensions.connectionId
- connection id.debugContext
- debug context.
-
onFragment
public void onFragment(BinaryFrame frame, boolean last)
This callback will be invoked when a fragmented binary message has been received.- Parameters:
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.
-
onFragment
public void onFragment(TextFrame frame, boolean last)
This callback will be invoked when a fragmented textual message has been received.- Parameters:
frame
- the text received from the remote endpoint.last
- flag indicating whether or not the payload received is the final fragment of a message.
-
onMessage
public void onMessage(BinaryFrame frame)
This callback will be invoked when a binary message has been received.- Parameters:
frame
- the binary data received from the remote endpoint.
-
onMessage
public void onMessage(TextFrame frame)
This callback will be invoked when a text message has been received.- Parameters:
frame
- the text received from the remote endpoint.
-
onPing
public void onPing(PingFrame frame)
This callback will be invoked when the remote endpoint has sent a ping frame.- Parameters:
frame
- the ping frame from the remote endpoint.
-
onPong
public void onPong(PongFrame frame)
This callback will be invoked when the remote endpoint has sent a pong frame.- Parameters:
frame
- the pong frame from the remote endpoint.
-
close
public void close()
Closes thisTyrusWebSocket
.
-
close
public void close(int code, java.lang.String reason)
Closes thisTyrusWebSocket
using the specified status code and reason.- Parameters:
code
- the closing status code.reason
- the reason, if any.
-
close
public void close(CloseReason closeReason)
Closes thisTyrusWebSocket
using theCloseReason
.- Parameters:
closeReason
- the close reason.
-
sendBinary
@Deprecated public java.util.concurrent.Future<Frame> sendBinary(byte[] data)
Deprecated.Send a binary frame to the remote endpoint.- Parameters:
data
- data to be sent.- Returns:
Future
which could be used to control/check the sending completion state.
-
sendBinary
public java.util.concurrent.Future<Frame> sendBinary(byte[] data, WriterInfo writerInfo)
Send a binary frame to the remote endpoint.- Parameters:
data
- data to be sent.writerInfo
- information about the outbound message.- Returns:
Future
which could be used to control/check the sending completion state.
-
sendBinary
@Deprecated public void sendBinary(byte[] data, SendHandler handler)
Deprecated.Send a binary frame to the remote endpoint.- Parameters:
data
- data to be sent.handler
-SendHandler.onResult(javax.websocket.SendResult)
will be called when sending is complete.
-
sendBinary
public void sendBinary(byte[] data, SendHandler handler, WriterInfo writerInfo)
Send a binary frame to the remote endpoint.- Parameters:
data
- data to be sent.writerInfo
- information about the outbound message.handler
-SendHandler.onResult(javax.websocket.SendResult)
will be called when sending is complete.
-
sendText
@Deprecated public java.util.concurrent.Future<Frame> sendText(java.lang.String data)
Deprecated.Send a text frame to the remote endpoint.- Parameters:
data
- data to be sent.- Returns:
Future
which could be used to control/check the sending completion state.
-
sendText
public java.util.concurrent.Future<Frame> sendText(java.lang.String data, WriterInfo writerInfo)
Send a text frame to the remote endpoint.- Parameters:
data
- data to be sent.writerInfo
- information about the outbound message.- Returns:
Future
which could be used to control/check the sending completion state.
-
sendText
@Deprecated public void sendText(java.lang.String data, SendHandler handler)
Deprecated.Send a text frame to the remote endpoint.- Parameters:
data
- data to be sent.handler
-SendHandler.onResult(javax.websocket.SendResult)
will be called when sending is complete.
-
sendText
public void sendText(java.lang.String data, SendHandler handler, WriterInfo writerInfo)
Send a text frame to the remote endpoint.- Parameters:
data
- data to be sent.writerInfo
- information about the outbound message.handler
-SendHandler.onResult(javax.websocket.SendResult)
will be called when sending is complete.
-
sendRawFrame
public java.util.concurrent.Future<Frame> sendRawFrame(java.nio.ByteBuffer data)
Send a frame to the remote endpoint.- Parameters:
data
- complete data frame.- Returns:
Future
which could be used to control/check the sending completion state.
-
sendPing
public java.util.concurrent.Future<Frame> sendPing(byte[] data)
Sends aping
frame with the specified payload (if any).- Parameters:
data
- optional payload. Note that payload length is restricted to 125 bytes or less.- Returns:
Future
which could be used to control/check the sending completion state.
-
sendPong
public java.util.concurrent.Future<Frame> sendPong(byte[] data)
Sends aping
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."
- Parameters:
data
- optional payload. Note that payload length is restricted to 125 bytes or less.- Returns:
Future
which could be used to control/check the sending completion state.
-
sendText
@Deprecated public java.util.concurrent.Future<Frame> sendText(java.lang.String fragment, boolean last)
Deprecated.Sends a fragment of a complete message.- Parameters:
fragment
- the textual fragment to send.last
- boolean indicating if this message fragment is the last.- Returns:
Future
which could be used to control/check the sending completion state.
-
sendText
public java.util.concurrent.Future<Frame> sendText(java.lang.String fragment, boolean last, WriterInfo writerInfo)
Sends a fragment of a complete message.- Parameters:
fragment
- the textual fragment to send.last
- boolean indicating if this message fragment is the last.writerInfo
- information about the outbound message.- Returns:
Future
which could be used to control/check the sending completion state.
-
sendBinary
@Deprecated public java.util.concurrent.Future<Frame> sendBinary(byte[] bytes, boolean last)
Deprecated.Sends a fragment of a complete message.- Parameters:
bytes
- the binary fragment to send.last
- boolean indicating if this message fragment is the last.- Returns:
Future
which could be used to control/check the sending completion state.
-
sendBinary
public java.util.concurrent.Future<Frame> sendBinary(byte[] bytes, boolean last, WriterInfo writerInfo)
Sends a fragment of a complete message.- Parameters:
bytes
- the binary fragment to send.last
- boolean indicating if this message fragment is the last.writerInfo
- information about the outbound message.- Returns:
Future
which could be used to control/check the sending completion state.
-
sendBinary
@Deprecated public java.util.concurrent.Future<Frame> sendBinary(byte[] bytes, int off, int len, boolean last)
Deprecated.Sends a fragment of a complete message.- Parameters:
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.- Returns:
Future
which could be used to control/check the sending completion state.
-
sendBinary
public java.util.concurrent.Future<Frame> sendBinary(byte[] bytes, int off, int len, boolean last, WriterInfo writerInfo)
Sends a fragment of a complete message.- Parameters:
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.writerInfo
- information about the outbound message.- Returns:
Future
which could be used to control/check the sending completion state.
-
-