public class SimpleWebSocket extends Object implements WebSocket
| Modifier and Type | Class and Description |
|---|---|
protected static class |
SimpleWebSocket.State |
| Modifier and Type | Field and Description |
|---|---|
protected Broadcaster |
broadcaster |
protected EnumSet<SimpleWebSocket.State> |
connected |
protected Queue<WebSocketListener> |
listeners |
protected ProtocolHandler |
protocolHandler |
protected AtomicReference<SimpleWebSocket.State> |
state |
ABNORMAL_CLOSE, END_POINT_GOING_DOWN, INVALID_DATA, MESSAGE_TOO_LARGE, NO_STATUS_CODE, NORMAL_CLOSURE, PROTOCOL_ERROR| Constructor and Description |
|---|
SimpleWebSocket(ProtocolHandler protocolHandler,
WebSocketListener... listeners) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(WebSocketListener listener)
Adds a
WebSocketListener to be notified of events of interest. |
void |
broadcast(Iterable<? extends WebSocket> recipients,
byte[] data)
Broadcasts the data to the remote end-point set.
|
void |
broadcast(Iterable<? extends WebSocket> recipients,
String data)
Broadcasts the data to the remote end-point set.
|
void |
broadcastFragment(Iterable<? extends WebSocket> recipients,
byte[] data,
boolean last)
Broadcasts the data fragment to the remote end-point set.
|
void |
broadcastFragment(Iterable<? extends WebSocket> recipients,
String data,
boolean last)
Broadcasts the data fragment to the remote end-point set.
|
void |
close()
Closes this
WebSocket. |
void |
close(int code)
Closes this
WebSocket using the specified status code. |
void |
close(int code,
String reason)
Closes this
WebSocket using the specified status code and
reason. |
protected Broadcaster |
getBroadcaster() |
Collection<WebSocketListener> |
getListeners() |
boolean |
isConnected()
Convenience method to determine if this
WebSocket is connected. |
void |
onClose(DataFrame frame)
This callback will be invoked when the remote end-point sent a closing
frame.
|
void |
onConnect()
This callback will be invoked when the opening handshake between both
endpoints has been completed.
|
void |
onFragment(boolean last,
byte[] fragment)
This callback will be invoked when a fragmented binary message has
been received.
|
void |
onFragment(boolean last,
String fragment)
This callback will be invoked when a fragmented textual message has
been received.
|
void |
onMessage(byte[] data)
This callback will be invoked when a binary message has been received.
|
void |
onMessage(String text)
This callback will be invoked when a text message has been received.
|
void |
onPing(DataFrame frame)
This callback will be invoked when the remote end-point has sent a ping
frame.
|
void |
onPong(DataFrame frame)
This callback will be invoked when the remote end-point has sent a pong
frame.
|
boolean |
remove(WebSocketListener listener)
Removes the specified
WebSocketListener as 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(String data)
Send a text frame to the remote end-point.
|
org.glassfish.grizzly.GrizzlyFuture<DataFrame> |
sendPing(byte[] data)
Sends a
ping frame with the specified payload (if any). |
org.glassfish.grizzly.GrizzlyFuture<DataFrame> |
sendPong(byte[] data)
Sends a
ping frame with the specified payload (if any). |
protected void |
sendRaw(byte[] rawData) |
protected void |
setBroadcaster(Broadcaster broadcaster) |
void |
setClosed() |
org.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> |
stream(boolean last,
String fragment)
Sends a fragment of a complete message.
|
protected byte[] |
toRawData(byte[] binary) |
protected byte[] |
toRawData(byte[] binary,
boolean last) |
protected byte[] |
toRawData(String text) |
protected byte[] |
toRawData(String fragment,
boolean last) |
protected final Queue<WebSocketListener> listeners
protected final ProtocolHandler protocolHandler
protected Broadcaster broadcaster
protected final EnumSet<SimpleWebSocket.State> connected
protected final AtomicReference<SimpleWebSocket.State> state
public SimpleWebSocket(ProtocolHandler protocolHandler, WebSocketListener... listeners)
public Collection<WebSocketListener> getListeners()
public final boolean add(WebSocketListener listener)
WebSocketWebSocketListener to be notified of events of interest.add in interface WebSocketlistener - the WebSocketListener to add.true if the listener was added, otherwise
falseWebSocketListenerpublic final boolean remove(WebSocketListener listener)
WebSocketWebSocketListener as a target of event
notification.remove in interface WebSocketlistener - the WebSocketListener to remote.true if the listener was removed, otherwise
falseWebSocketListenerpublic boolean isConnected()
WebSocket
Convenience method to determine if this WebSocket is connected.
isConnected in interface WebSockettrue if the WebSocket is connected, otherwise
falsepublic void setClosed()
public void onClose(DataFrame frame)
WebSocketThis callback will be invoked when the remote end-point sent a closing frame.
public void onConnect()
WebSocketThis callback will be invoked when the opening handshake between both endpoints has been completed.
public void onFragment(boolean last,
byte[] fragment)
WebSocketThis callback will be invoked when a fragmented binary message has been received.
onFragment in interface WebSocketlast - 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.public void onFragment(boolean last,
String fragment)
WebSocketThis callback will be invoked when a fragmented textual message has been received.
onFragment in interface WebSocketlast - flag indicating whether or not the payload received is the
final fragment of a message.fragment - the text received from the remote end-point.public void onMessage(byte[] data)
WebSocketThis callback will be invoked when a binary message has been received.
public void onMessage(String text)
WebSocketThis callback will be invoked when a text message has been received.
public void onPing(DataFrame frame)
WebSocketThis callback will be invoked when the remote end-point has sent a ping frame.
public void onPong(DataFrame frame)
WebSocketThis callback will be invoked when the remote end-point has sent a pong frame.
public void close(int code)
WebSocket
Closes this WebSocket using the specified status code.
public void close(int code,
String reason)
WebSocket
Closes this WebSocket using the specified status code and
reason.
public org.glassfish.grizzly.GrizzlyFuture<DataFrame> send(byte[] data)
WebSocketSend a binary frame to the remote end-point.
public org.glassfish.grizzly.GrizzlyFuture<DataFrame> send(String data)
WebSocketSend a text frame to the remote end-point.
public void broadcast(Iterable<? extends WebSocket> recipients, String data)
WebSocketBroadcasts the data to the remote end-point set.
public void broadcast(Iterable<? extends WebSocket> recipients, byte[] data)
WebSocketBroadcasts the data to the remote end-point set.
public void broadcastFragment(Iterable<? extends WebSocket> recipients, String data, boolean last)
WebSocketBroadcasts the data fragment to the remote end-point set.
broadcastFragment in interface WebSocketpublic void broadcastFragment(Iterable<? extends WebSocket> recipients, byte[] data, boolean last)
WebSocketBroadcasts the data fragment to the remote end-point set.
broadcastFragment in interface WebSocketpublic org.glassfish.grizzly.GrizzlyFuture<DataFrame> sendPing(byte[] data)
ping frame with the specified payload (if any).
public org.glassfish.grizzly.GrizzlyFuture<DataFrame> 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."
public org.glassfish.grizzly.GrizzlyFuture<DataFrame> stream(boolean last, String fragment)
WebSocketSends a fragment of a complete message.
public org.glassfish.grizzly.GrizzlyFuture<DataFrame> stream(boolean last, byte[] bytes, int off, int len)
WebSocketSends a fragment of a complete message.
stream in interface WebSocketlast - 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.GrizzlyFuture which could be used to control/check the sending completion state.protected byte[] toRawData(String text)
protected byte[] toRawData(byte[] binary)
protected byte[] toRawData(String fragment, boolean last)
protected byte[] toRawData(byte[] binary,
boolean last)
protected void sendRaw(byte[] rawData)
protected Broadcaster getBroadcaster()
protected void setBroadcaster(Broadcaster broadcaster)
Copyright © 2018 Oracle Corporation. All Rights Reserved.