Class SimpleWebSocket
- All Implemented Interfaces:
WebSocket
- Direct Known Subclasses:
DefaultWebSocket
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Broadcasterprotected final EnumSet<SimpleWebSocket.State>protected final Queue<WebSocketListener>protected final ProtocolHandlerprotected final AtomicReference<SimpleWebSocket.State>Fields inherited from interface org.glassfish.grizzly.websockets.WebSocket
ABNORMAL_CLOSE, END_POINT_GOING_DOWN, INVALID_DATA, MESSAGE_TOO_LARGE, NO_STATUS_CODE, NORMAL_CLOSURE, PROTOCOL_ERROR -
Constructor Summary
ConstructorsConstructorDescriptionSimpleWebSocket(ProtocolHandler protocolHandler, WebSocketListener... listeners) -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanadd(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.completableClose(int code, String reason) protected BroadcasterbooleanConvenience 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[] fragment) This callback will be invoked when a fragmented binary message has been received.voidonFragment(boolean last, String fragment) 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.final 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<org.glassfish.grizzly.WriteResult>sendRaw(byte[] rawData) protected voidsetBroadcaster(Broadcaster broadcaster) voidorg.glassfish.grizzly.GrizzlyFuture<DataFrame>stream(boolean last, byte[] bytes, int off, int len) Sends a fragment of a complete message.org.glassfish.grizzly.GrizzlyFuture<DataFrame>Sends a fragment of a complete message.byte[]toRawData(byte[] binary) byte[]toRawData(byte[] binary, boolean last) byte[]byte[]
-
Field Details
-
listeners
-
protocolHandler
-
broadcaster
-
connected
-
state
-
-
Constructor Details
-
SimpleWebSocket
-
-
Method Details
-
getListeners
-
add
Description copied from interface:WebSocketAdds aWebSocketListenerto be notified of events of interest.- Specified by:
addin interfaceWebSocket- Parameters:
listener- theWebSocketListenerto add.- Returns:
trueif the listener was added, otherwisefalse- See Also:
-
remove
Description copied from interface:WebSocketRemoves the specifiedWebSocketListeneras a target of event notification.- Specified by:
removein interfaceWebSocket- Parameters:
listener- theWebSocketListenerto remote.- Returns:
trueif the listener was removed, otherwisefalse- See Also:
-
isConnected
public boolean isConnected()Description copied from interface:WebSocketConvenience method to determine if this
WebSocketis connected.- Specified by:
isConnectedin interfaceWebSocket- Returns:
trueif theWebSocketis connected, otherwisefalse
-
setClosed
public void setClosed() -
onClose
Description copied from interface:WebSocketThis callback will be invoked when the remote end-point sent a closing frame.
-
onConnect
public void onConnect()Description copied from interface:WebSocketThis callback will be invoked when the opening handshake between both endpoints has been completed.
-
onFragment
public void onFragment(boolean last, byte[] fragment) Description copied from interface:WebSocketThis callback will be invoked when a fragmented binary message has been received.
- Specified by:
onFragmentin interfaceWebSocket- Parameters:
last- flag indicating whether or not the payload received is the final fragment of a message.fragment- the binary data received from the remote end-point.
-
onFragment
Description copied from interface:WebSocketThis callback will be invoked when a fragmented textual message has been received.
- Specified by:
onFragmentin interfaceWebSocket- Parameters:
last- flag indicating whether or not the payload received is the final fragment of a message.fragment- the text received from the remote end-point.
-
onMessage
public void onMessage(byte[] data) Description copied from interface:WebSocketThis callback will be invoked when a binary message has been received.
-
onMessage
Description copied from interface:WebSocketThis callback will be invoked when a text message has been received.
-
onPing
Description copied from interface:WebSocketThis callback will be invoked when the remote end-point has sent a ping frame.
-
onPong
Description copied from interface:WebSocketThis callback will be invoked when the remote end-point has sent a pong frame.
-
close
public void close()Description copied from interface:WebSocketCloses this
WebSocket. -
close
public void close(int code) Description copied from interface:WebSocketCloses this
WebSocketusing the specified status code. -
close
Description copied from interface:WebSocketCloses this
WebSocketusing the specified status code and reason. -
completableClose
- Specified by:
completableClosein interfaceWebSocket
-
send
Description copied from interface:WebSocketSend a binary frame to the remote end-point.
-
send
Description copied from interface:WebSocketSend a text frame to the remote end-point.
-
broadcast
Description copied from interface:WebSocketBroadcasts the data to the remote end-point set.
-
broadcast
Description copied from interface:WebSocketBroadcasts the data to the remote end-point set.
-
broadcastFragment
Description copied from interface:WebSocketBroadcasts the data fragment to the remote end-point set.
- Specified by:
broadcastFragmentin interfaceWebSocket
-
broadcastFragment
Description copied from interface:WebSocketBroadcasts the data fragment to the remote end-point set.
- Specified by:
broadcastFragmentin interfaceWebSocket
-
sendPing
Sends apingframe with the specified payload (if any). -
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."
-
stream
Description copied from interface:WebSocketSends a fragment of a complete message.
-
stream
public org.glassfish.grizzly.GrizzlyFuture<DataFrame> stream(boolean last, byte[] bytes, int off, int len) Description copied from interface:WebSocketSends a fragment of a complete message.
- Specified by:
streamin interfaceWebSocket- Parameters:
last- boolean indicating if this message fragment is the 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.- Returns:
GrizzlyFuturewhich could be used to control/check the sending completion state.
-
toRawData
-
toRawData
public byte[] toRawData(byte[] binary) -
toRawData
-
toRawData
public byte[] toRawData(byte[] binary, boolean last) -
sendRaw
public org.glassfish.grizzly.GrizzlyFuture<org.glassfish.grizzly.WriteResult> sendRaw(byte[] rawData) -
getBroadcaster
-
setBroadcaster
-