Class SimpleWebSocket

java.lang.Object
org.glassfish.grizzly.websockets.SimpleWebSocket
All Implemented Interfaces:
WebSocket
Direct Known Subclasses:
DefaultWebSocket

public class SimpleWebSocket extends Object implements WebSocket
Generic WebSocket implementation. Typically used by client implementations.
  • Field Details

  • Constructor Details

  • Method Details

    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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.
    • completableClose

      public CompletableFuture<DataFrame> completableClose(int code, String reason)
      Specified by:
      completableClose in interface WebSocket
    • send

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

      public void broadcast(Iterable<? extends WebSocket> recipients, String data)
      Description copied from interface: WebSocket

      Broadcasts the data to the remote end-point set.

      Specified by:
      broadcast in interface WebSocket
    • broadcast

      public void broadcast(Iterable<? extends WebSocket> recipients, byte[] data)
      Description copied from interface: WebSocket

      Broadcasts the data to the remote end-point set.

      Specified by:
      broadcast in interface WebSocket
    • broadcastFragment

      public void broadcastFragment(Iterable<? extends WebSocket> recipients, String data, boolean last)
      Description copied from interface: WebSocket

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

      Specified by:
      broadcastFragment in interface WebSocket
    • broadcastFragment

      public void broadcastFragment(Iterable<? extends WebSocket> recipients, byte[] data, boolean last)
      Description copied from interface: WebSocket

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

      Specified by:
      broadcastFragment in interface WebSocket
    • sendPing

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

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

      public byte[] toRawData(String text)
    • toRawData

      public byte[] toRawData(byte[] binary)
    • toRawData

      public byte[] toRawData(String fragment, boolean last)
    • toRawData

      public byte[] toRawData(byte[] binary, boolean last)
    • sendRaw

      public org.glassfish.grizzly.GrizzlyFuture<org.glassfish.grizzly.WriteResult> sendRaw(byte[] rawData)
    • getBroadcaster

      protected Broadcaster getBroadcaster()
    • setBroadcaster

      protected void setBroadcaster(Broadcaster broadcaster)