Interface WebSocket
- All Known Implementing Classes:
DefaultWebSocket,SimpleWebSocket
- Author:
- Alexey Stashok
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final inta reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint.static final intIndicates that an endpoint is "going away", such as a server going down, or a browser having navigated away from a page.static final intIndicates that an endpoint is terminating the connection because it has received a type of data it cannot accept (e.g.static final intindicates that an endpoint is terminating the connection because it has received a message that is too large.static final inta reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint.static final intIndicates a normal closure, meaning whatever purpose the connection was established for has been fulfilled.static final intIndicates that an endpoint is terminating the connection due to a protocol error. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(WebSocketListener listener) Adds aWebSocketListenerto be notified of events of interest.voidBroadcasts the data to the remote end-point set.voidBroadcasts the data to the remote end-point set.voidbroadcastFragment(Iterable<? extends WebSocket> recipients, byte[] data, boolean last) Broadcasts the data fragment to the remote end-point set.voidbroadcastFragment(Iterable<? extends WebSocket> recipients, String data, boolean last) Broadcasts the data fragment to the remote end-point set.voidclose()Closes thisWebSocket.voidclose(int code) Closes thisWebSocketusing the specified status code.voidCloses thisWebSocketusing the specified status code and reason.booleanConvenience method to determine if thisWebSocketis connected.voidThis callback will be invoked when the remote end-point sent a closing frame.voidThis callback will be invoked when the opening handshake between both endpoints has been completed.voidonFragment(boolean last, byte[] payload) This callback will be invoked when a fragmented binary message has been received.voidonFragment(boolean last, String payload) This callback will be invoked when a fragmented textual message has been received.voidonMessage(byte[] data) This callback will be invoked when a binary message has been received.voidThis callback will be invoked when a text message has been received.voidThis callback will be invoked when the remote end-point has sent a ping frame.voidThis callback will be invoked when the remote end-point has sent a pong frame.booleanremove(WebSocketListener listener) Removes the specifiedWebSocketListeneras a target of event notification.org.glassfish.grizzly.GrizzlyFuture<DataFrame>send(byte[] data) Send a binary frame to the remote end-point.org.glassfish.grizzly.GrizzlyFuture<DataFrame>Send a text frame to the remote end-point.org.glassfish.grizzly.GrizzlyFuture<DataFrame>sendPing(byte[] data) Sends apingframe with the specified payload (if any).org.glassfish.grizzly.GrizzlyFuture<DataFrame>sendPong(byte[] data) Sends apingframe with the specified payload (if any).org.glassfish.grizzly.GrizzlyFuture<DataFrame>stream(boolean last, byte[] fragment, int off, int len) Sends a fragment of a complete message.org.glassfish.grizzly.GrizzlyFuture<DataFrame>Sends a fragment of a complete message.
-
Field Details
-
NORMAL_CLOSURE
static final int NORMAL_CLOSUREIndicates a normal closure, meaning whatever purpose the connection was established for has been fulfilled.- See Also:
-
END_POINT_GOING_DOWN
static final int END_POINT_GOING_DOWNIndicates that an endpoint is "going away", such as a server going down, or a browser having navigated away from a page.- See Also:
-
PROTOCOL_ERROR
static final int PROTOCOL_ERRORIndicates that an endpoint is terminating the connection due to a protocol error.- See Also:
-
INVALID_DATA
static final int INVALID_DATAIndicates that an endpoint is terminating the connection because it has received a type of data it cannot accept (e.g. an endpoint that understands only text data may send this if it receives a binary message.)- See Also:
-
MESSAGE_TOO_LARGE
static final int MESSAGE_TOO_LARGEindicates that an endpoint is terminating the connection because it has received a message that is too large.- See Also:
-
NO_STATUS_CODE
static final int NO_STATUS_CODEa reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. It is designated for use in applications expecting a status code to indicate that no status code was actually present.- See Also:
-
ABNORMAL_CLOSE
static final int ABNORMAL_CLOSEa reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. It is designated for use in applications expecting a status code to indicate that the connection was closed abnormally, e.g. without sending or receiving a Close control frame.- See Also:
-
-
Method Details
-
send
Send a text frame to the remote end-point.
- Returns:
GrizzlyFuturewhich could be used to control/check the sending completion state.
-
send
Send a binary frame to the remote end-point.
- Returns:
GrizzlyFuturewhich could be used to control/check the sending completion state.
-
broadcast
Broadcasts the data to the remote end-point set.
- Parameters:
recipients-data-
-
broadcast
Broadcasts the data to the remote end-point set.
- Parameters:
recipients-data-
-
broadcastFragment
Broadcasts the data fragment to the remote end-point set.
- Parameters:
recipients-data-
-
broadcastFragment
Broadcasts the data fragment to the remote end-point set.
- Parameters:
recipients-data-
-
sendPing
Sends apingframe with the specified payload (if any).- Parameters:
data- optional payload. Note that payload length is restricted to 125 bytes or less.- Returns:
GrizzlyFuturewhich could be used to control/check the sending completion state.- Since:
- 2.1.9
-
sendPong
Sends a
pingframe 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:
GrizzlyFuturewhich could be used to control/check the sending completion state.- Since:
- 2.1.9
-
stream
Sends a fragment of a complete message.
- Parameters:
last- boolean indicating if this message fragment is the last.fragment- the textual fragment to send.- Returns:
GrizzlyFuturewhich could be used to control/check the sending completion state.
-
stream
org.glassfish.grizzly.GrizzlyFuture<DataFrame> stream(boolean last, byte[] fragment, int off, int len) Sends a fragment of a complete message.
- Parameters:
last- boolean indicating if this message fragment is the last.fragment- the binary fragment to send.off- the offset within the fragment to send.len- the number of bytes of the fragment to send.- Returns:
GrizzlyFuturewhich could be used to control/check the sending completion state.
-
close
void close()Closes this
WebSocket. -
close
void close(int code) Closes this
WebSocketusing the specified status code.- Parameters:
code- the closing status code.
-
close
Closes this
WebSocketusing the specified status code and reason.- Parameters:
code- the closing status code.reason- the reason, if any.
-
isConnected
boolean isConnected()Convenience method to determine if this
WebSocketis connected.- Returns:
trueif theWebSocketis connected, otherwisefalse
-
onConnect
void onConnect()This callback will be invoked when the opening handshake between both endpoints has been completed.
-
onMessage
This callback will be invoked when a text message has been received.
- Parameters:
text- the text received from the remote end-point.
-
onMessage
void onMessage(byte[] data) This callback will be invoked when a binary message has been received.
- Parameters:
data- the binary data received from the remote end-point.
-
onFragment
This callback will be invoked when a fragmented textual message has been received.
- Parameters:
last- flag indicating whether or not the payload received is the final fragment of a message.payload- the text received from the remote end-point.
-
onFragment
void onFragment(boolean last, byte[] payload) This callback will be invoked when a fragmented binary message has been received.
- Parameters:
last- flag indicating whether or not the payload received is the final fragment of a message.payload- the binary data received from the remote end-point.
-
onClose
This callback will be invoked when the remote end-point sent a closing frame.
- Parameters:
frame- the close frame from the remote end-point.- See Also:
-
onPing
This callback will be invoked when the remote end-point has sent a ping frame.
- Parameters:
frame- the ping frame from the remote end-point.- See Also:
-
onPong
This callback will be invoked when the remote end-point has sent a pong frame.
- Parameters:
frame- the pong frame from the remote end-point.- See Also:
-
add
Adds aWebSocketListenerto be notified of events of interest.- Parameters:
listener- theWebSocketListenerto add.- Returns:
trueif the listener was added, otherwisefalse- See Also:
-
remove
Removes the specifiedWebSocketListeneras a target of event notification.- Parameters:
listener- theWebSocketListenerto remote.- Returns:
trueif the listener was removed, otherwisefalse- See Also:
-