Interface WebSocket

All Known Implementing Classes:
DefaultWebSocket, SimpleWebSocket

public interface WebSocket
General WebSocket unit interface.
Author:
Alexey Stashok
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint.
    static final int
    Indicates that an endpoint is "going away", such as a server going down, or a browser having navigated away from a page.
    static final int
    Indicates that an endpoint is terminating the connection because it has received a type of data it cannot accept (e.g.
    static final int
    indicates that an endpoint is terminating the connection because it has received a message that is too large.
    static final int
    a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint.
    static final int
    Indicates a normal closure, meaning whatever purpose the connection was established for has been fulfilled.
    static final int
    Indicates that an endpoint is terminating the connection due to a protocol error.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    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
    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.
    completableClose(int code, String reason)
     
    boolean
    Convenience method to determine if this WebSocket is connected.
    void
    This callback will be invoked when the remote end-point sent a closing frame.
    void
    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
    This callback will be invoked when a text message has been received.
    void
    This callback will be invoked when the remote end-point has sent a ping frame.
    void
    This callback will be invoked when the remote end-point has sent a pong frame.
    boolean
    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).
    org.glassfish.grizzly.GrizzlyFuture<DataFrame>
    stream(boolean last, byte[] fragment, 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.
  • Field Details

    • NORMAL_CLOSURE

      static final int NORMAL_CLOSURE
      Indicates a normal closure, meaning whatever purpose the connection was established for has been fulfilled.
      See Also:
    • 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:
    • PROTOCOL_ERROR

      static final int PROTOCOL_ERROR
      Indicates that an endpoint is terminating the connection due to a protocol error.
      See Also:
    • 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:
    • 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:
    • 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:
    • 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:
  • Method Details

    • send

      org.glassfish.grizzly.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

      org.glassfish.grizzly.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.
    • broadcast

      void broadcast(Iterable<? extends WebSocket> recipients, String data)

      Broadcasts the data to the remote end-point set.

      Parameters:
      recipients -
      data -
    • broadcast

      void broadcast(Iterable<? extends WebSocket> recipients, byte[] data)

      Broadcasts the data to the remote end-point set.

      Parameters:
      recipients -
      data -
    • broadcastFragment

      void broadcastFragment(Iterable<? extends WebSocket> recipients, String data, boolean last)

      Broadcasts the data fragment to the remote end-point set.

      Parameters:
      recipients -
      data -
    • broadcastFragment

      void broadcastFragment(Iterable<? extends WebSocket> recipients, byte[] data, boolean last)

      Broadcasts the data fragment to the remote end-point set.

      Parameters:
      recipients -
      data -
    • sendPing

      org.glassfish.grizzly.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

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

      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

      org.glassfish.grizzly.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

      org.glassfish.grizzly.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.
    • completableClose

      CompletableFuture<DataFrame> completableClose(int code, String reason)
    • 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:
    • 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:
    • 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:
    • 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:
    • 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: