Top Description Inners Fields Constructors Methods
org.glassfish.tyrus.core

public Class TyrusWebSocket

extends Object
Class Inheritance
Imports
java.nio.ByteBuffer, java.util.EnumSet, .List, java.util.concurrent.CountDownLatch, .Future, java.util.concurrent.atomic.AtomicReference, java.util.concurrent.locks.Lock, .ReentrantLock, jakarta.websocket.CloseReason, .Extension, .SendHandler, org.glassfish.tyrus.core.frame.BinaryFrame, .CloseFrame, .Frame, .PingFrame, .PongFrame, .TextFrame, .TyrusFrame, org.glassfish.tyrus.core.l10n.LocalizationMessages, org.glassfish.tyrus.core.monitoring.MessageEventListener, org.glassfish.tyrus.spi.UpgradeRequest, .WriterInfo

Tyrus representation of web socket connection.

Instance of this class represents one bi-directional websocket connection.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum

Field Summary

Modifier and TypeField and Description
private final EnumSet<TyrusWebSocket.State>
private final TyrusEndpointWrapper
private final Lock
private volatile MessageEventListener
private final CountDownLatch
private static final WriterInfo
private static final WriterInfo
private final ProtocolHandler
private final AtomicReference<TyrusWebSocket.State>

Constructor Summary

AccessConstructor and Description
public
TyrusWebSocket(final ProtocolHandler
used for writing data (sending).
protocolHandler
,
final TyrusEndpointWrapper
notifies registered endpoints about incoming events.
endpointWrapper
)

Create new instance, set ProtocolHandler and register TyrusEndpointWrapper.

Method Summary

Modifier and TypeMethod and Description
private void
private void
public void
close()

Closes this TyrusWebSocket.

public void
close(int
the closing status code.
code
,
String
the reason, if any.
reason
)

Closes this TyrusWebSocket using the specified status code and reason.

public void
close(CloseReason
the close reason.
closeReason
)

Closes this TyrusWebSocket using the jakarta.websocket.CloseReason.

pack-priv MessageEventListener

Returns:

message event listener.
getMessageEventListener
()

Get message event listener.

pack-priv ProtocolHandler
public boolean

Returns:

true if the TyrusWebSocket is connected, false otherwise.
isConnected
()

Convenience method to determine if this TyrusWebSocket instance is connected.

public void
onClose(CloseFrame
the close frame from the remote endpoint.
frame
)

This callback will be invoked when the remote endpoint sent a closing frame.

public void
onConnect(UpgradeRequest
request associated with this socket.
upgradeRequest
,
String
negotiated subprotocol.
subProtocol
,
List<Extension>
negotiated extensions.
extensions
,
String
connection id.
connectionId
,
DebugContext
debug context.
debugContext
)

This callback will be invoked when the opening handshake between both endpoints has been completed.

public void
onFragment(BinaryFrame
the binary data received from the remote endpoint.
frame
,
boolean
flag indicating whether or not the payload received is the final fragment of a message.
last
)

This callback will be invoked when a fragmented binary message has been received.

public void
onFragment(TextFrame
the text received from the remote endpoint.
frame
,
boolean
flag indicating whether or not the payload received is the final fragment of a message.
last
)

This callback will be invoked when a fragmented textual message has been received.

public void
onMessage(BinaryFrame
the binary data received from the remote endpoint.
frame
)

This callback will be invoked when a binary message has been received.

public void
onMessage(TextFrame
the text received from the remote endpoint.
frame
)

This callback will be invoked when a text message has been received.

public void
onPing(PingFrame
the ping frame from the remote endpoint.
frame
)

This callback will be invoked when the remote endpoint has sent a ping frame.

public void
onPong(PongFrame
the pong frame from the remote endpoint.
frame
)

This callback will be invoked when the remote endpoint has sent a pong frame.

private Future<Frame>
send(TyrusFrame frame, WriterInfo writerInfo)

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendBinary
(byte[]
data to be sent.
data
)
Deprecated

Send a binary frame to the remote endpoint.

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendBinary
(byte[]
data to be sent.
data
,
WriterInfo
information about the outbound message.
writerInfo
)

Send a binary frame to the remote endpoint.

public void
sendBinary(byte[]
data to be sent.
data
,
SendHandler
SendHandler#onResult(jakarta.websocket.SendResult) will be called when sending is complete.
handler
)
Deprecated

Send a binary frame to the remote endpoint.

public void
sendBinary(byte[]
data to be sent.
data
,
SendHandler
SendHandler#onResult(jakarta.websocket.SendResult) will be called when sending is complete.
handler
,
WriterInfo
information about the outbound message.
writerInfo
)

Send a binary frame to the remote endpoint.

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendBinary
(byte[]
the binary fragment to send.
bytes
,
boolean
boolean indicating if this message fragment is the last.
last
)
Deprecated

Sends a fragment of a complete message.

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendBinary
(byte[]
the binary fragment to send.
bytes
,
boolean
boolean indicating if this message fragment is the last.
last
,
WriterInfo
information about the outbound message.
writerInfo
)

Sends a fragment of a complete message.

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendBinary
(byte[]
the binary fragment to send.
bytes
,
int
the offset within the fragment to send.
off
,
int
the number of bytes of the fragment to send.
len
,
boolean
boolean indicating if this message fragment is the last.
last
)
Deprecated

Sends a fragment of a complete message.

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendBinary
(byte[]
the binary fragment to send.
bytes
,
int
the offset within the fragment to send.
off
,
int
the number of bytes of the fragment to send.
len
,
boolean
boolean indicating if this message fragment is the last.
last
,
WriterInfo
information about the outbound message.
writerInfo
)

Sends a fragment of a complete message.

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendPing
(byte[]
optional payload. Note that payload length is restricted to 125 bytes or less.
data
)

Sends a ping frame with the specified payload (if any).

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendPong
(byte[]
optional payload. Note that payload length is restricted to 125 bytes or less.
data
)

Sends a ping frame with the specified payload (if any).

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendRawFrame
(ByteBuffer
complete data frame.
data
)

Send a frame to the remote endpoint.

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendText
(String
data to be sent.
data
)
Deprecated

Send a text frame to the remote endpoint.

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendText
(String
data to be sent.
data
,
WriterInfo
information about the outbound message.
writerInfo
)

Send a text frame to the remote endpoint.

public void
sendText(String
data to be sent.
data
,
SendHandler
SendHandler#onResult(jakarta.websocket.SendResult) will be called when sending is complete.
handler
)
Deprecated

Send a text frame to the remote endpoint.

public void
sendText(String
data to be sent.
data
,
SendHandler
SendHandler#onResult(jakarta.websocket.SendResult) will be called when sending is complete.
handler
,
WriterInfo
information about the outbound message.
writerInfo
)

Send a text frame to the remote endpoint.

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendText
(String
the textual fragment to send.
fragment
,
boolean
boolean indicating if this message fragment is the last.
last
)
Deprecated

Sends a fragment of a complete message.

public Future<Frame>

Returns:

Future which could be used to control/check the sending completion state.
sendText
(String
the textual fragment to send.
fragment
,
boolean
boolean indicating if this message fragment is the last.
last
,
WriterInfo
information about the outbound message.
writerInfo
)

Sends a fragment of a complete message.

pack-priv void
setMessageEventListener(MessageEventListener
message event listener.
messageEventListener
)

Set message event listener.

public void
setWriteTimeout(long
timeout in milliseconds.
timeoutMs
)

Sets the timeout for the writing operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

connectedback to summary
private final EnumSet<TyrusWebSocket.State> connected
endpointWrapperback to summary
private final TyrusEndpointWrapper endpointWrapper
lockback to summary
private final Lock lock
messageEventListenerback to summary
private volatile MessageEventListener messageEventListener
onConnectLatchback to summary
private final CountDownLatch onConnectLatch
PING_INFOback to summary
private static final WriterInfo PING_INFO
PONG_INFOback to summary
private static final WriterInfo PONG_INFO
protocolHandlerback to summary
private final ProtocolHandler protocolHandler
stateback to summary
private final AtomicReference<TyrusWebSocket.State> state

Constructor Detail

TyrusWebSocketback to summary
public TyrusWebSocket(final ProtocolHandler protocolHandler, final TyrusEndpointWrapper endpointWrapper)

Create new instance, set ProtocolHandler and register TyrusEndpointWrapper.

Parameters
protocolHandler:ProtocolHandler

used for writing data (sending).

endpointWrapper:TyrusEndpointWrapper

notifies registered endpoints about incoming events.

Method Detail

awaitOnConnectback to summary
private void awaitOnConnect()
checkConnectedStateback to summary
private void checkConnectedState()
closeback to summary
public void close()

Closes this TyrusWebSocket.

closeback to summary
public void close(int code, String reason)

Closes this TyrusWebSocket using the specified status code and reason.

Parameters
code:int

the closing status code.

reason:String

the reason, if any.

closeback to summary
public void close(CloseReason closeReason)

Closes this TyrusWebSocket using the jakarta.websocket.CloseReason.

Parameters
closeReason:CloseReason

the close reason.

getMessageEventListenerback to summary
pack-priv MessageEventListener getMessageEventListener()

Get message event listener.

Returns:MessageEventListener

message event listener.

getProtocolHandlerback to summary
pack-priv ProtocolHandler getProtocolHandler()
isConnectedback to summary
public boolean isConnected()

Convenience method to determine if this TyrusWebSocket instance is connected.

Returns:boolean

true if the TyrusWebSocket is connected, false otherwise.

onCloseback to summary
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:CloseFrame

the close frame from the remote endpoint.

onConnectback to summary
public 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.

Parameters
upgradeRequest:UpgradeRequest

request associated with this socket.

subProtocol:String

negotiated subprotocol.

extensions:List<Extension>

negotiated extensions.

connectionId:String

connection id.

debugContext:DebugContext

debug context.

onFragmentback to summary
public void onFragment(BinaryFrame frame, boolean last)

This callback will be invoked when a fragmented binary message has been received.

Parameters
frame:BinaryFrame

the binary data received from the remote endpoint.

last:boolean

flag indicating whether or not the payload received is the final fragment of a message.

onFragmentback to summary
public void onFragment(TextFrame frame, boolean last)

This callback will be invoked when a fragmented textual message has been received.

Parameters
frame:TextFrame

the text received from the remote endpoint.

last:boolean

flag indicating whether or not the payload received is the final fragment of a message.

onMessageback to summary
public void onMessage(BinaryFrame frame)

This callback will be invoked when a binary message has been received.

Parameters
frame:BinaryFrame

the binary data received from the remote endpoint.

onMessageback to summary
public void onMessage(TextFrame frame)

This callback will be invoked when a text message has been received.

Parameters
frame:TextFrame

the text received from the remote endpoint.

onPingback to summary
public void onPing(PingFrame frame)

This callback will be invoked when the remote endpoint has sent a ping frame.

Parameters
frame:PingFrame

the ping frame from the remote endpoint.

onPongback to summary
public void onPong(PongFrame frame)

This callback will be invoked when the remote endpoint has sent a pong frame.

Parameters
frame:PongFrame

the pong frame from the remote endpoint.

sendback to summary
private Future<Frame> send(TyrusFrame frame, WriterInfo writerInfo)
sendBinaryback to summary
public Future<Frame> sendBinary(byte[] data)

Deprecated

Send a binary frame to the remote endpoint.

Parameters
data:byte[]

data to be sent.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

Annotations
@Deprecated
sendBinaryback to summary
public Future<Frame> sendBinary(byte[] data, WriterInfo writerInfo)

Send a binary frame to the remote endpoint.

Parameters
data:byte[]

data to be sent.

writerInfo:WriterInfo

information about the outbound message.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

sendBinaryback to summary
public void sendBinary(byte[] data, SendHandler handler)

Deprecated

Send a binary frame to the remote endpoint.

Parameters
data:byte[]

data to be sent.

handler:SendHandler

SendHandler#onResult(jakarta.websocket.SendResult) will be called when sending is complete.

Annotations
@Deprecated
sendBinaryback to summary
public void sendBinary(byte[] data, SendHandler handler, WriterInfo writerInfo)

Send a binary frame to the remote endpoint.

Parameters
data:byte[]

data to be sent.

handler:SendHandler

SendHandler#onResult(jakarta.websocket.SendResult) will be called when sending is complete.

writerInfo:WriterInfo

information about the outbound message.

sendBinaryback to summary
public Future<Frame> sendBinary(byte[] bytes, boolean last)

Deprecated

Sends a fragment of a complete message.

Parameters
bytes:byte[]

the binary fragment to send.

last:boolean

boolean indicating if this message fragment is the last.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

Annotations
@Deprecated
sendBinaryback to summary
public Future<Frame> sendBinary(byte[] bytes, boolean last, WriterInfo writerInfo)

Sends a fragment of a complete message.

Parameters
bytes:byte[]

the binary fragment to send.

last:boolean

boolean indicating if this message fragment is the last.

writerInfo:WriterInfo

information about the outbound message.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

sendBinaryback to summary
public Future<Frame> sendBinary(byte[] bytes, int off, int len, boolean last)

Deprecated

Sends a fragment of a complete message.

Parameters
bytes:byte[]

the binary fragment to send.

off:int

the offset within the fragment to send.

len:int

the number of bytes of the fragment to send.

last:boolean

boolean indicating if this message fragment is the last.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

Annotations
@Deprecated
sendBinaryback to summary
public Future<Frame> sendBinary(byte[] bytes, int off, int len, boolean last, WriterInfo writerInfo)

Sends a fragment of a complete message.

Parameters
bytes:byte[]

the binary fragment to send.

off:int

the offset within the fragment to send.

len:int

the number of bytes of the fragment to send.

last:boolean

boolean indicating if this message fragment is the last.

writerInfo:WriterInfo

information about the outbound message.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

sendPingback to summary
public Future<Frame> sendPing(byte[] data)

Sends a ping frame with the specified payload (if any).

Parameters
data:byte[]

optional payload. Note that payload length is restricted to 125 bytes or less.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

sendPongback to summary
public Future<Frame> sendPong(byte[] data)

Sends a 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."

Parameters
data:byte[]

optional payload. Note that payload length is restricted to 125 bytes or less.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

sendRawFrameback to summary
public Future<Frame> sendRawFrame(ByteBuffer data)

Send a frame to the remote endpoint.

Parameters
data:ByteBuffer

complete data frame.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

sendTextback to summary
public Future<Frame> sendText(String data)

Deprecated

Send a text frame to the remote endpoint.

Parameters
data:String

data to be sent.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

Annotations
@Deprecated
sendTextback to summary
public Future<Frame> sendText(String data, WriterInfo writerInfo)

Send a text frame to the remote endpoint.

Parameters
data:String

data to be sent.

writerInfo:WriterInfo

information about the outbound message.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

sendTextback to summary
public void sendText(String data, SendHandler handler)

Deprecated

Send a text frame to the remote endpoint.

Parameters
data:String

data to be sent.

handler:SendHandler

SendHandler#onResult(jakarta.websocket.SendResult) will be called when sending is complete.

Annotations
@Deprecated
sendTextback to summary
public void sendText(String data, SendHandler handler, WriterInfo writerInfo)

Send a text frame to the remote endpoint.

Parameters
data:String

data to be sent.

handler:SendHandler

SendHandler#onResult(jakarta.websocket.SendResult) will be called when sending is complete.

writerInfo:WriterInfo

information about the outbound message.

sendTextback to summary
public Future<Frame> sendText(String fragment, boolean last)

Deprecated

Sends a fragment of a complete message.

Parameters
fragment:String

the textual fragment to send.

last:boolean

boolean indicating if this message fragment is the last.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

Annotations
@Deprecated
sendTextback to summary
public Future<Frame> sendText(String fragment, boolean last, WriterInfo writerInfo)

Sends a fragment of a complete message.

Parameters
fragment:String

the textual fragment to send.

last:boolean

boolean indicating if this message fragment is the last.

writerInfo:WriterInfo

information about the outbound message.

Returns:Future<Frame>

Future which could be used to control/check the sending completion state.

setMessageEventListenerback to summary
pack-priv void setMessageEventListener(MessageEventListener messageEventListener)

Set message event listener.

Parameters
messageEventListener:MessageEventListener

message event listener.

setWriteTimeoutback to summary
public void setWriteTimeout(long timeoutMs)

Sets the timeout for the writing operation.

Parameters
timeoutMs:long

timeout in milliseconds.

org.glassfish.tyrus.core back to summary

private final Enum TyrusWebSocket.State

extends Enum<TyrusWebSocket.State>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final TyrusWebSocket.State
public static final TyrusWebSocket.State
public static final TyrusWebSocket.State
public static final TyrusWebSocket.State

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static TyrusWebSocket.State
public static TyrusWebSocket.State[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

CLOSEDback to summary
public static final TyrusWebSocket.State CLOSED
CLOSINGback to summary
public static final TyrusWebSocket.State CLOSING
CONNECTEDback to summary
public static final TyrusWebSocket.State CONNECTED
NEWback to summary
public static final TyrusWebSocket.State NEW

Constructor Detail

Stateback to summary
private State()

Method Detail

valueOfback to summary
public static TyrusWebSocket.State valueOf(String name)
valuesback to summary
public static TyrusWebSocket.State[] values()