Class WebSocketApplication

java.lang.Object
org.glassfish.grizzly.websockets.WebSocketAdapter
org.glassfish.grizzly.websockets.WebSocketApplication
All Implemented Interfaces:
WebSocketListener

public abstract class WebSocketApplication extends WebSocketAdapter
Abstract server-side WebSocket application, which will handle application WebSockets events.
Author:
Alexey Stashok
  • Field Details

  • Constructor Details

    • WebSocketApplication

      public WebSocketApplication()
  • Method Details

    • createSocket

      @Deprecated public WebSocket createSocket(ProtocolHandler handler, WebSocketListener... listeners)
      Factory method to create new WebSocket instances. Developers may wish to override this to return customized WebSocket implementations.
      Parameters:
      handler - the ProtocolHandler to use with the newly created WebSocket.
      listeners - the WebSocketListeners to associate with the new WebSocket.
      Returns:
      a new WebSocket instance.
    • createSocket

      public WebSocket createSocket(ProtocolHandler handler, org.glassfish.grizzly.http.HttpRequestPacket requestPacket, WebSocketListener... listeners)
      Factory method to create new WebSocket instances. Developers may wish to override this to return customized WebSocket implementations.
      Parameters:
      handler - the ProtocolHandler to use with the newly created WebSocket.
      requestPacket - the HttpRequestPacket that triggered the creation of the WebSocket connection.
      listeners - the WebSocketListeners to associate with the new WebSocket.
      Returns:
    • onClose

      public void onClose(WebSocket socket, DataFrame frame)
      When a WebSocket.onClose(DataFrame) is invoked, the WebSocket will be unassociated with this application and closed. If this method is overridden, the overriding method must call remove(WebSocket). This is necessary to ensure WebSocket instances are not leaked nor are message operations against closed sockets are performed.
      Specified by:
      onClose in interface WebSocketListener
      Overrides:
      onClose in class WebSocketAdapter
      Parameters:
      socket - the WebSocket being closed.
      frame - the closing frame.
    • onConnect

      public void onConnect(WebSocket socket)
      When a new WebSocket connection is made to this application, the WebSocket will be associated with this application. If this method is overridden, the overriding method must call add(WebSocket). This is necessary to ensure bulk message sending via facilities such as Broadcaster function properly.
      Specified by:
      onConnect in interface WebSocketListener
      Overrides:
      onConnect in class WebSocketAdapter
      Parameters:
      socket - the new WebSocket connection.
    • onExtensionNegotiation

      public void onExtensionNegotiation(List<Extension> extensions)
      Invoked during the handshake if the client has advertised extensions it may use and one or more extensions intersect with those returned by getSupportedExtensions(). The Extensions passed to this method will include any extension parameters included by the client. It's up to this method to re-order and or adjust any parameter values within the list. This method must not add any extensions that weren't originally in the list, but it is acceptable to remove one or all extensions if for some reason they can't be supported. If not overridden, the List will be sent as-is back to the client.
      Parameters:
      extensions - the intersection of extensions between client and application.
      Since:
      2.3
    • upgrade

      public final boolean upgrade(org.glassfish.grizzly.http.HttpRequestPacket request)
      Checks protocol specific information can and should be upgraded. The default implementation will check for the presence of the Upgrade header with a value of WebSocket. If present, isApplicationRequest(org.glassfish.grizzly.http.HttpRequestPacket) will be invoked to determine if the request is a valid websocket request.
      Returns:
      true if the request should be upgraded to a WebSocket connection
    • isApplicationRequest

      public boolean isApplicationRequest(org.glassfish.grizzly.http.HttpRequestPacket request)
      Deprecated.
      URI mapping shouldn't be intrinsic to the application. WebSocketApplications should be registered using WebSocketEngine.register(String, String, WebSocketApplication) using standard Servlet url-pattern rules.
      Checks application specific criteria to determine if this application can process the request as a WebSocket connection.
      Parameters:
      request - the incoming HTTP request.
      Returns:
      true if this application can service this request

    • getSupportedExtensions

      public List<Extension> getSupportedExtensions()
      Return the websocket extensions supported by this WebSocketApplication. The Extensions added to this List should not include any Extension.Parameters as they will be ignored. This is used exclusively for matching the requested extensions.
      Returns:
      the websocket extensions supported by this WebSocketApplication.
    • getSupportedProtocols

      public List<String> getSupportedProtocols(List<String> subProtocol)
      Parameters:
      subProtocol -
      Returns:
    • getWebSockets

      protected Set<WebSocket> getWebSockets()
      Returns a set of WebSockets, registered with the application. The returned set is unmodifiable, the possible modifications may cause exceptions.
      Returns:
      a set of WebSockets, registered with the application.
    • add

      protected boolean add(WebSocket socket)
      Associates the specified WebSocket with this application.
      Parameters:
      socket - the WebSocket to associate with this application.
      Returns:
      true if the socket was successfully associated, otherwise returns false.
    • remove

      public boolean remove(WebSocket socket)
      Unassociates the specified WebSocket with this application.
      Parameters:
      socket - the WebSocket to unassociate with this application.
      Returns:
      true if the socket was successfully unassociated, otherwise returns false.
    • handshake

      protected void handshake(HandShake handshake) throws HandshakeException
      This method will be called, when initial WebSocket handshake process has been completed, but allows the application to perform further negotiation/validation.
      Throws:
      HandshakeException - error occurred during the handshake.
    • onError

      protected boolean onError(WebSocket webSocket, Throwable t)
      This method will be invoked if an unexpected exception is caught by the WebSocket runtime.
      Parameters:
      webSocket - the websocket being processed at the time the exception occurred.
      t - the unexpected exception.
      Returns:
      true if the WebSocket should be closed otherwise false.