org.glassfish.grizzly.websockets
Class DefaultWebSocket

java.lang.Object
  extended by org.glassfish.grizzly.websockets.DefaultWebSocket
All Implemented Interfaces:
WebSocket

public class DefaultWebSocket
extends Object
implements WebSocket


Field Summary
protected  ProtocolHandler protocolHandler
           
protected  HttpRequestPacket request
           
 
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
DefaultWebSocket(ProtocolHandler protocolHandler, HttpRequestPacket request, WebSocketListener... listeners)
           
DefaultWebSocket(ProtocolHandler protocolHandler, WebSocketListener... listeners)
           
 
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.
 Collection<WebSocketListener> getListeners()
           
 HttpRequestPacket getUpgradeRequest()
          Returns the upgrade request for this WebSocket.
 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).
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

protocolHandler

protected final ProtocolHandler protocolHandler

request

protected final HttpRequestPacket request
Constructor Detail

DefaultWebSocket

public DefaultWebSocket(ProtocolHandler protocolHandler,
                        WebSocketListener... listeners)

DefaultWebSocket

public DefaultWebSocket(ProtocolHandler protocolHandler,
                        HttpRequestPacket request,
                        WebSocketListener... listeners)
Method Detail

getUpgradeRequest

public HttpRequestPacket getUpgradeRequest()
Returns the upgrade request for this WebSocket.

Returns:
the upgrade request for this WebSocket. This method may return null depending on the context under which this WebSocket was created.

getListeners

public Collection<WebSocketListener> getListeners()

add

public final boolean add(WebSocketListener listener)
Description copied from interface: WebSocket
Adds a WebSocketListener to be notified of events of interest.

Specified by:
add in interface WebSocket
Parameters:
listener - the WebSocketListener to add.
Returns:
true if the listener was added, otherwise false
See Also:
WebSocketListener

remove

public final boolean remove(WebSocketListener listener)
Description copied from interface: WebSocket
Removes the specified WebSocketListener as a target of event notification.

Specified by:
remove in interface WebSocket
Parameters:
listener - the WebSocketListener to remote.
Returns:
true if the listener was removed, otherwise false
See Also:
WebSocketListener

isConnected

public boolean isConnected()
Description copied from interface: WebSocket

Convenience method to determine if this WebSocket is connected.

Specified by:
isConnected in interface WebSocket
Returns:
true if the WebSocket is connected, otherwise false

setClosed

public void setClosed()

onClose

public void onClose(DataFrame frame)
Description copied from interface: WebSocket

This callback will be invoked when the remote end-point sent a closing frame.

Specified by:
onClose in interface WebSocket
Parameters:
frame - the close frame from the remote end-point.
See Also:
DataFrame

onConnect

public void onConnect()
Description copied from interface: WebSocket

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

Specified by:
onConnect in interface WebSocket

onFragment

public void onFragment(boolean last,
                       byte[] fragment)
Description copied from interface: WebSocket

This callback will be invoked when a fragmented binary message has been received.

Specified by:
onFragment in interface WebSocket
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

public void onFragment(boolean last,
                       String fragment)
Description copied from interface: WebSocket

This callback will be invoked when a fragmented textual message has been received.

Specified by:
onFragment in interface WebSocket
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: WebSocket

This callback will be invoked when a binary message has been received.

Specified by:
onMessage in interface WebSocket
Parameters:
data - the binary data received from the remote end-point.

onMessage

public void onMessage(String text)
Description copied from interface: WebSocket

This callback will be invoked when a text message has been received.

Specified by:
onMessage in interface WebSocket
Parameters:
text - the text received from the remote end-point.

onPing

public void onPing(DataFrame frame)
Description copied from interface: WebSocket

This callback will be invoked when the remote end-point has sent a ping frame.

Specified by:
onPing in interface WebSocket
Parameters:
frame - the ping frame from the remote end-point.
See Also:
DataFrame

onPong

public void onPong(DataFrame frame)
Description copied from interface: WebSocket

This callback will be invoked when the remote end-point has sent a pong frame.

Specified by:
onPong in interface WebSocket
Parameters:
frame - the pong frame from the remote end-point.
See Also:
DataFrame

close

public void close()
Description copied from interface: WebSocket

Closes this WebSocket.

Specified by:
close in interface WebSocket

close

public void close(int code)
Description copied from interface: WebSocket

Closes this WebSocket using the specified status code.

Specified by:
close in interface WebSocket
Parameters:
code - the closing status code.

close

public void close(int code,
                  String reason)
Description copied from interface: WebSocket

Closes this WebSocket using the specified status code and reason.

Specified by:
close in interface WebSocket
Parameters:
code - the closing status code.
reason - the reason, if any.

send

public GrizzlyFuture<DataFrame> send(byte[] data)
Description copied from interface: WebSocket

Send a binary frame to the remote end-point.

Specified by:
send in interface WebSocket
Returns:
GrizzlyFuture which could be used to control/check the sending completion state.

send

public GrizzlyFuture<DataFrame> send(String data)
Description copied from interface: WebSocket

Send a text frame to the remote end-point.

Specified by:
send in interface WebSocket
Returns:
GrizzlyFuture which could be used to control/check the sending completion state.

sendPing

public GrizzlyFuture<DataFrame> sendPing(byte[] data)

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

Specified by:
sendPing in interface WebSocket
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.

sendPong

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

Specified by:
sendPong in interface WebSocket
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.

stream

public GrizzlyFuture<DataFrame> stream(boolean last,
                                       String fragment)
Description copied from interface: WebSocket

Sends a fragment of a complete message.

Specified by:
stream in interface WebSocket
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

public GrizzlyFuture<DataFrame> stream(boolean last,
                                       byte[] bytes,
                                       int off,
                                       int len)
Description copied from interface: WebSocket

Sends a fragment of a complete message.

Specified by:
stream in interface WebSocket
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:
GrizzlyFuture which could be used to control/check the sending completion state.


Copyright © 2012 Oracle Corporation. All Rights Reserved.