Class WebSocketClient

java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.ContainerLifeCycle
org.eclipse.jetty.websocket.client.WebSocketClient
All Implemented Interfaces:
org.eclipse.jetty.util.component.Container, org.eclipse.jetty.util.component.Destroyable, org.eclipse.jetty.util.component.Dumpable, org.eclipse.jetty.util.component.Dumpable.DumpableContainer, org.eclipse.jetty.util.component.LifeCycle, WebSocketContainerScope

public class WebSocketClient extends org.eclipse.jetty.util.component.ContainerLifeCycle implements WebSocketContainerScope
WebSocketClient provides a means of establishing connections to remote websocket endpoints.
  • Field Details

    • LOG

      private static final org.eclipse.jetty.util.log.Logger LOG
    • httpClient

      private final org.eclipse.jetty.client.HttpClient httpClient
    • objectFactorySupplier

      private final Supplier<org.eclipse.jetty.util.DecoratedObjectFactory> objectFactorySupplier
    • classloader

      private final ClassLoader classloader
    • policy

      private final WebSocketPolicy policy
    • extensionRegistry

      private final WebSocketExtensionFactory extensionRegistry
    • sessionTracker

      private final SessionTracker sessionTracker
    • sessionListeners

      private final List<WebSocketSessionListener> sessionListeners
    • eventDriverFactory

      private EventDriverFactory eventDriverFactory
    • sessionFactory

      private SessionFactory sessionFactory
    • stopAtShutdown

      private boolean stopAtShutdown
  • Constructor Details

    • WebSocketClient

      public WebSocketClient()
      Instantiate a WebSocketClient with defaults.
    • WebSocketClient

      public WebSocketClient(org.eclipse.jetty.client.HttpClient httpClient)
      Instantiate a WebSocketClient using provided HttpClient.
      Parameters:
      httpClient - the HttpClient to use for WebSocketClient.
    • WebSocketClient

      public WebSocketClient(org.eclipse.jetty.client.HttpClient httpClient, org.eclipse.jetty.util.DecoratedObjectFactory decoratedObjectFactory)
      Instantiate a WebSocketClient using HttpClient for defaults
      Parameters:
      httpClient - the HttpClient that underlying WebSocket client uses
      decoratedObjectFactory - the DecoratedObjectFactory for all client instantiated classes
    • WebSocketClient

      @Deprecated public WebSocketClient(org.eclipse.jetty.util.ssl.SslContextFactory sslContextFactory)
      Deprecated.
      Create a new WebSocketClient
      Parameters:
      sslContextFactory - ssl context factory to use on the internal HttpClient
    • WebSocketClient

      @Deprecated public WebSocketClient(Executor executor)
      Deprecated.
      Create a new WebSocketClient
      Parameters:
      executor - the executor to use on the internal HttpClient
    • WebSocketClient

      @Deprecated public WebSocketClient(org.eclipse.jetty.io.ByteBufferPool bufferPool)
      Deprecated.
      Create a new WebSocketClient
      Parameters:
      bufferPool - byte buffer pool to use on the internal HttpClient
    • WebSocketClient

      @Deprecated public WebSocketClient(org.eclipse.jetty.util.ssl.SslContextFactory sslContextFactory, Executor executor)
      Deprecated.
      Create a new WebSocketClient
      Parameters:
      sslContextFactory - ssl context factory to use on the internal HttpClient
      executor - the executor to use on the internal HttpClient
    • WebSocketClient

      @Deprecated public WebSocketClient(WebSocketContainerScope scope)
      Deprecated.
      Create WebSocketClient other Container Scope, to allow sharing of internal features like Executor, ByteBufferPool, SSLContextFactory, etc.
      Parameters:
      scope - the Container Scope
    • WebSocketClient

      @Deprecated public WebSocketClient(WebSocketContainerScope scope, org.eclipse.jetty.util.ssl.SslContextFactory sslContextFactory)
      Deprecated.
      Create WebSocketClient other Container Scope, to allow sharing of internal features like Executor, ByteBufferPool, SSLContextFactory, etc.
      Parameters:
      scope - the Container Scope
      sslContextFactory - SSL ContextFactory to use in preference to one from WebSocketContainerScope.getSslContextFactory()
    • WebSocketClient

      @Deprecated public WebSocketClient(org.eclipse.jetty.util.ssl.SslContextFactory sslContextFactory, Executor executor, org.eclipse.jetty.io.ByteBufferPool bufferPool)
      Deprecated.
      Create WebSocketClient using sharing instances of SSLContextFactory Executor, and ByteBufferPool
      Parameters:
      sslContextFactory - shared SSL ContextFactory
      executor - shared Executor
      bufferPool - shared ByteBufferPool
    • WebSocketClient

      @Deprecated public WebSocketClient(WebSocketContainerScope scope, EventDriverFactory eventDriverFactory, SessionFactory sessionFactory)
      Create WebSocketClient based on pre-existing Container Scope, to allow sharing of internal features like Executor, ByteBufferPool, SSLContextFactory, etc.
      Parameters:
      scope - the Container Scope
      eventDriverFactory - the EventDriver Factory to use
      sessionFactory - the SessionFactory to use
    • WebSocketClient

      public WebSocketClient(WebSocketContainerScope scope, EventDriverFactory eventDriverFactory, SessionFactory sessionFactory, org.eclipse.jetty.client.HttpClient httpClient)
      Create WebSocketClient based on pre-existing Container Scope, to allow sharing of internal features like Executor, ByteBufferPool, SSLContextFactory, etc.
      Parameters:
      scope - the Container Scope
      eventDriverFactory - the EventDriver Factory to use
      sessionFactory - the SessionFactory to use
      httpClient - the httpClient to use
  • Method Details

    • newHttpClient

      private static org.eclipse.jetty.client.HttpClient newHttpClient(org.eclipse.jetty.util.ssl.SslContextFactory sslContextFactory, Executor executor, org.eclipse.jetty.io.ByteBufferPool bufferPool)
    • connect

      public Future<Session> connect(Object websocket, URI toUri) throws IOException
      Throws:
      IOException
    • connect

      public Future<Session> connect(Object websocket, URI toUri, ClientUpgradeRequest request) throws IOException
      Connect to remote websocket endpoint
      Parameters:
      websocket - the websocket object
      toUri - the websocket uri to connect to
      request - the upgrade request information
      Returns:
      the future for the session, available on success of connect
      Throws:
      IOException - if unable to connect
    • connect

      public Future<Session> connect(Object websocket, URI toUri, ClientUpgradeRequest request, UpgradeListener upgradeListener) throws IOException
      Connect to remote websocket endpoint
      Parameters:
      websocket - the websocket object
      toUri - the websocket uri to connect to
      request - the upgrade request information
      upgradeListener - the upgrade listener
      Returns:
      the future for the session, available on success of connect
      Throws:
      IOException - if unable to connect
    • getClassLoader

      public ClassLoader getClassLoader()
      Description copied from interface: WebSocketContainerScope

      The ClassLoader used to load classes for the WebSocketSession.

      By default this will be the ContextClassLoader at the time this method is called. However this will be overridden by the WebSocketClient to use the ContextClassLoader at the time it was created, this is because the client uses its own ThreadPool so the WebSocketSessions may be created when the ContextClassLoader is not set.

      Specified by:
      getClassLoader in interface WebSocketContainerScope
      Returns:
      the classloader.
    • setEventDriverFactory

      public void setEventDriverFactory(EventDriverFactory eventDriverFactory)
    • setSessionFactory

      public void setSessionFactory(SessionFactory sessionFactory)
    • doStart

      protected void doStart() throws Exception
      Overrides:
      doStart in class org.eclipse.jetty.util.component.ContainerLifeCycle
      Throws:
      Exception
    • doStop

      protected void doStop() throws Exception
      Overrides:
      doStop in class org.eclipse.jetty.util.component.ContainerLifeCycle
      Throws:
      Exception
    • isDispatchIO

      @Deprecated public boolean isDispatchIO()
      Deprecated.
    • getAsyncWriteTimeout

      @Deprecated public long getAsyncWriteTimeout()
      Deprecated.
      Return the number of milliseconds for a timeout of an attempted write operation.
      Returns:
      number of milliseconds for timeout of an attempted write operation
    • getBindAddress

      public SocketAddress getBindAddress()
    • getBufferPool

      public org.eclipse.jetty.io.ByteBufferPool getBufferPool()
      Description copied from interface: WebSocketContainerScope
      The configured Container Buffer Pool.
      Specified by:
      getBufferPool in interface WebSocketContainerScope
      Returns:
      the buffer pool (never null)
    • getConnectionManager

      @Deprecated public ConnectionManager getConnectionManager()
      Deprecated.
    • getConnectTimeout

      public long getConnectTimeout()
    • getCookieStore

      public CookieStore getCookieStore()
    • getEventDriverFactory

      public EventDriverFactory getEventDriverFactory()
    • getExecutor

      public Executor getExecutor()
      Description copied from interface: WebSocketContainerScope
      Executor in use by the container.
      Specified by:
      getExecutor in interface WebSocketContainerScope
      Returns:
      the Executor in use by the container.
    • getExtensionFactory

      public ExtensionFactory getExtensionFactory()
    • getMasker

      @Deprecated public Masker getMasker()
      Deprecated.
      not used, no replacement
      Returns:
      a RandomMasker instance
    • getMaxBinaryMessageBufferSize

      public int getMaxBinaryMessageBufferSize()
      Get the maximum size for buffering of a binary message.
      Returns:
      the maximum size of a binary message buffer.
    • getMaxBinaryMessageSize

      public long getMaxBinaryMessageSize()
      Get the maximum size for a binary message.
      Returns:
      the maximum size of a binary message.
    • getMaxIdleTimeout

      public long getMaxIdleTimeout()
      Get the max idle timeout for new connections.
      Returns:
      the max idle timeout in milliseconds for new connections.
    • getMaxTextMessageBufferSize

      public int getMaxTextMessageBufferSize()
      Get the maximum size for buffering of a text message.
      Returns:
      the maximum size of a text message buffer.
    • getMaxTextMessageSize

      public long getMaxTextMessageSize()
      Get the maximum size for a text message.
      Returns:
      the maximum size of a text message.
    • getObjectFactory

      public org.eclipse.jetty.util.DecoratedObjectFactory getObjectFactory()
      Description copied from interface: WebSocketContainerScope
      Object Factory used to create objects.
      Specified by:
      getObjectFactory in interface WebSocketContainerScope
      Returns:
      Object Factory used to create instances of objects.
    • getOpenSessions

      public Set<WebSocketSession> getOpenSessions()
    • getPolicy

      public WebSocketPolicy getPolicy()
      Description copied from interface: WebSocketContainerScope
      The policy the container is running on.
      Specified by:
      getPolicy in interface WebSocketContainerScope
      Returns:
      the websocket policy
    • getScheduler

      public org.eclipse.jetty.util.thread.Scheduler getScheduler()
    • getSessionFactory

      public SessionFactory getSessionFactory()
    • getSslContextFactory

      public org.eclipse.jetty.util.ssl.SslContextFactory getSslContextFactory()
      Get the in use SslContextFactory
      Specified by:
      getSslContextFactory in interface WebSocketContainerScope
      Returns:
      the SslContextFactory that manages TLS encryption on the internal HttpClient
      See Also:
    • addSessionListener

      public void addSessionListener(WebSocketSessionListener listener)
      Specified by:
      addSessionListener in interface WebSocketContainerScope
    • removeSessionListener

      public void removeSessionListener(WebSocketSessionListener listener)
      Specified by:
      removeSessionListener in interface WebSocketContainerScope
    • getSessionListeners

      public Collection<WebSocketSessionListener> getSessionListeners()
      Specified by:
      getSessionListeners in interface WebSocketContainerScope
    • init

      private void init()
    • newConnectionManager

      @Deprecated protected ConnectionManager newConnectionManager()
      Deprecated.
      has no replacement
      Factory method for new ConnectionManager
      Returns:
      the ConnectionManager instance to use
    • setAsyncWriteTimeout

      public void setAsyncWriteTimeout(long ms)
    • setBindAdddress

      @Deprecated public void setBindAdddress(SocketAddress bindAddress)
      Deprecated.
      (this is a bad bad bad typo, it has 3 { @ code " d " } characters in a row) use HttpClient.setBindAddress(SocketAddress) to the internal WebSocketClient(HttpClient)
      Parameters:
      bindAddress - the address to bind to the internal HttpClient
    • setBindAddress

      public void setBindAddress(SocketAddress bindAddress)
      Sets the Bind Address on the internal HttpClient.
      Parameters:
      bindAddress - the local bind address for the internal HttpClient
    • setBufferPool

      public void setBufferPool(org.eclipse.jetty.io.ByteBufferPool bufferPool)
      Set's the Bind Address on the internal HttpClient.
      Parameters:
      bufferPool - The buffer pool
    • setConnectTimeout

      public void setConnectTimeout(long ms)
      Set the timeout for connecting to the remote server on the internal HttpClient
      Parameters:
      ms - the timeout in milliseconds
    • setCookieStore

      public void setCookieStore(CookieStore cookieStore)
      Set the CookieStore to use on the internal HttpClient
      Parameters:
      cookieStore - The cookie store
    • setDaemon

      @Deprecated public void setDaemon(boolean daemon)
      Deprecated.
      not used, configure threading in HttpClient instead
      Parameters:
      daemon - do nothing
    • setDispatchIO

      @Deprecated public void setDispatchIO(boolean dispatchIO)
      Deprecated.
      no longer used, this has no replacement
      Parameters:
      dispatchIO - true to have IO operations be dispatched to Executor
    • setExecutor

      public void setExecutor(Executor executor)
      Sets the Executor in use on the internal HttpClient
      Parameters:
      executor - The executor to use
    • setMasker

      @Deprecated public void setMasker(Masker masker)
      Deprecated.
      not used, no replacement
      Parameters:
      masker - does nothing
    • setMaxBinaryMessageBufferSize

      public void setMaxBinaryMessageBufferSize(int max)
    • setMaxIdleTimeout

      public void setMaxIdleTimeout(long ms)
      Set the max idle timeout for new connections.

      Existing connections will not have their max idle timeout adjusted.

      Parameters:
      ms - the timeout in milliseconds
    • setMaxTextMessageBufferSize

      public void setMaxTextMessageBufferSize(int max)
    • getHttpClient

      public org.eclipse.jetty.client.HttpClient getHttpClient()
      Get the internal HttpClient.

      Note: this can result in a LinkageError if used within a WebApp that runs on a server that also has HttpClient on the server classpath.

      Returns:
      the internal HttpClient
    • setStopAtShutdown

      public void setStopAtShutdown(boolean stop)
      Set JVM shutdown behavior.
      Parameters:
      stop - If true, this client instance will be explicitly stopped when the JVM is shutdown. Otherwise the application is responsible for maintaining the WebSocketClient lifecycle.
      See Also:
    • isStopAtShutdown

      public boolean isStopAtShutdown()
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class org.eclipse.jetty.util.component.AbstractLifeCycle