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. |
GrizzlyFuture<DataFrame> |
send(byte[] data)
Send a binary frame to the remote end-point.
|
GrizzlyFuture<DataFrame> |
send(String data)
Send a text frame to the remote end-point.
|
GrizzlyFuture<DataFrame> |
sendPing(byte[] data)
Sends a
ping frame with the specified payload (if any). |
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() |
GrizzlyFuture<DataFrame> |
stream(boolean last,
byte[] bytes,
int off,
int len)
Sends a fragment of a complete message.
|
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 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)
WebSocket
WebSocketListener
to be notified of events of interest.add
in interface WebSocket
listener
- the WebSocketListener
to add.true
if the listener was added, otherwise
false
WebSocketListener
public final boolean remove(WebSocketListener listener)
WebSocket
WebSocketListener
as a target of event
notification.remove
in interface WebSocket
listener
- the WebSocketListener
to remote.true
if the listener was removed, otherwise
false
WebSocketListener
public boolean isConnected()
WebSocket
Convenience method to determine if this WebSocket
is connected.
isConnected
in interface WebSocket
true
if the WebSocket
is connected, otherwise
false
public void setClosed()
public void onClose(DataFrame frame)
WebSocket
This callback will be invoked when the remote end-point sent a closing frame.
public void onConnect()
WebSocket
This callback will be invoked when the opening handshake between both endpoints has been completed.
public void onFragment(boolean last, byte[] fragment)
WebSocket
This callback will be invoked when a fragmented binary message has been received.
onFragment
in interface WebSocket
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.public void onFragment(boolean last, String fragment)
WebSocket
This callback will be invoked when a fragmented textual message has been received.
onFragment
in interface WebSocket
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.public void onMessage(byte[] data)
WebSocket
This callback will be invoked when a binary message has been received.
public void onMessage(String text)
WebSocket
This callback will be invoked when a text message has been received.
public void onPing(DataFrame frame)
WebSocket
This callback will be invoked when the remote end-point has sent a ping frame.
public void onPong(DataFrame frame)
WebSocket
This 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 GrizzlyFuture<DataFrame> send(byte[] data)
WebSocket
Send a binary frame to the remote end-point.
send
in interface WebSocket
GrizzlyFuture
which could be used to control/check the sending completion state.public GrizzlyFuture<DataFrame> send(String data)
WebSocket
Send a text frame to the remote end-point.
send
in interface WebSocket
GrizzlyFuture
which could be used to control/check the sending completion state.public void broadcast(Iterable<? extends WebSocket> recipients, String data)
WebSocket
Broadcasts the data to the remote end-point set.
public void broadcast(Iterable<? extends WebSocket> recipients, byte[] data)
WebSocket
Broadcasts the data to the remote end-point set.
public void broadcastFragment(Iterable<? extends WebSocket> recipients, String data, boolean last)
WebSocket
Broadcasts the data fragment to the remote end-point set.
broadcastFragment
in interface WebSocket
public void broadcastFragment(Iterable<? extends WebSocket> recipients, byte[] data, boolean last)
WebSocket
Broadcasts the data fragment to the remote end-point set.
broadcastFragment
in interface WebSocket
public GrizzlyFuture<DataFrame> sendPing(byte[] data)
ping
frame with the specified payload (if any).
sendPing
in interface WebSocket
data
- optional payload. Note that payload length is restricted
to 125 bytes or less.GrizzlyFuture
which could be used to control/check the sending completion state.public 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."
sendPong
in interface WebSocket
data
- optional payload. Note that payload length is restricted
to 125 bytes or less.GrizzlyFuture
which could be used to control/check the sending completion state.public GrizzlyFuture<DataFrame> stream(boolean last, String fragment)
WebSocket
Sends a fragment of a complete message.
stream
in interface WebSocket
last
- boolean indicating if this message fragment is the last.fragment
- the textual fragment to send.GrizzlyFuture
which could be used to control/check the sending completion state.public GrizzlyFuture<DataFrame> stream(boolean last, byte[] bytes, int off, int len)
WebSocket
Sends a fragment of a complete message.
stream
in interface WebSocket
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.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 © 2014 Oracle Corporation. All rights reserved.