org.glassfish.grizzly.websockets
Interface WebSocket

All Known Implementing Classes:
ChatWebSocket, DefaultWebSocket

public interface WebSocket

General WebSocket unit interface.

Author:
Alexey Stashok

Field Summary
static int ABNORMAL_CLOSE
          a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint.
static int END_POINT_GOING_DOWN
          Indicates that an endpoint is "going away", such as a server going down, or a browser having navigated away from a page.
static int INVALID_DATA
          Indicates that an endpoint is terminating the connection because it has received a type of data it cannot accept (e.g.
static int MESSAGE_TOO_LARGE
          indicates that an endpoint is terminating the connection because it has received a message that is too large.
static int NO_STATUS_CODE
          a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint.
static int NORMAL_CLOSURE
          Indicates a normal closure, meaning whatever purpose the connection was established for has been fulfilled.
static int PROTOCOL_ERROR
          Indicates that an endpoint is terminating the connection due to a protocol error.
 
Method Summary
 boolean add(WebSocketListener listener)
          Adds a WebSocketListener to be notified of events of interest.
 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.
 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[] payload)
           This callback will be invoked when a fragmented binary message has been received.
 void onFragment(boolean last, String payload)
           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).
 GrizzlyFuture<DataFrame> stream(boolean last, byte[] fragment, 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.
 

Field Detail

NORMAL_CLOSURE

static final int NORMAL_CLOSURE
Indicates a normal closure, meaning whatever purpose the connection was established for has been fulfilled.

See Also:
Constant Field Values

END_POINT_GOING_DOWN

static final int END_POINT_GOING_DOWN
Indicates that an endpoint is "going away", such as a server going down, or a browser having navigated away from a page.

See Also:
Constant Field Values

PROTOCOL_ERROR

static final int PROTOCOL_ERROR
Indicates that an endpoint is terminating the connection due to a protocol error.

See Also:
Constant Field Values

INVALID_DATA

static final int INVALID_DATA
Indicates 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:
Constant Field Values

MESSAGE_TOO_LARGE

static final int MESSAGE_TOO_LARGE
indicates that an endpoint is terminating the connection because it has received a message that is too large.

See Also:
Constant Field Values

NO_STATUS_CODE

static final int NO_STATUS_CODE
a 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:
Constant Field Values

ABNORMAL_CLOSE

static final int ABNORMAL_CLOSE
a 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:
Constant Field Values
Method Detail

send

GrizzlyFuture<DataFrame> send(String data)

Send a text frame to the remote end-point.

Returns:
GrizzlyFuture which could be used to control/check the sending completion state.

send

GrizzlyFuture<DataFrame> send(byte[] data)

Send a binary frame to the remote end-point.

Returns:
GrizzlyFuture which could be used to control/check the sending completion state.

sendPing

GrizzlyFuture<DataFrame> sendPing(byte[] data)

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

Parameters:
data - optional payload. Note that payload length is restricted to 125 bytes or less.
Returns:
GrizzlyFuture which could be used to control/check the sending completion state.
Since:
2.1.9

sendPong

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

Parameters:
data - optional payload. Note that payload length is restricted to 125 bytes or less.
Returns:
GrizzlyFuture which could be used to control/check the sending completion state.
Since:
2.1.9

stream

GrizzlyFuture<DataFrame> stream(boolean last,
                                String fragment)

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:
GrizzlyFuture which could be used to control/check the sending completion state.

stream

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:
GrizzlyFuture which could be used to control/check the sending completion state.

close

void close()

Closes this WebSocket.


close

void close(int code)

Closes this WebSocket using the specified status code.

Parameters:
code - the closing status code.

close

void close(int code,
           String reason)

Closes this WebSocket using 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 WebSocket is connected.

Returns:
true if the WebSocket is connected, otherwise false

onConnect

void onConnect()

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


onMessage

void onMessage(String text)

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

void onFragment(boolean last,
                String payload)

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

void onClose(DataFrame frame)

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:
DataFrame

onPing

void onPing(DataFrame frame)

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:
DataFrame

onPong

void onPong(DataFrame frame)

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:
DataFrame

add

boolean add(WebSocketListener listener)
Adds a WebSocketListener to be notified of events of interest.

Parameters:
listener - the WebSocketListener to add.
Returns:
true if the listener was added, otherwise false
See Also:
WebSocketListener

remove

boolean remove(WebSocketListener listener)
Removes the specified WebSocketListener as a target of event notification.

Parameters:
listener - the WebSocketListener to remote.
Returns:
true if the listener was removed, otherwise false
See Also:
WebSocketListener


Copyright © 2013 Oracle Corporation. All Rights Reserved.