Class MicronautWebSocketContainer

java.lang.Object
io.micronaut.servlet.websocket.MicronautWebSocketContainer
All Implemented Interfaces:
jakarta.websocket.WebSocketContainer

@Singleton @Requires(classes=jakarta.websocket.WebSocketContainer.class) @Requires(property="micronaut.servlet.websocket.enabled",notEquals="false",defaultValue="true") public final class MicronautWebSocketContainer extends Object implements jakarta.websocket.WebSocketContainer
The Jakarta WebSocketContainer of a Micronaut servlet application, for opening client connections.

Inject it and call connectToServer(Class, URI) with a @ClientEndpoint class compiled with the Micronaut servlet annotation processor. The connection is opened by the servlet container's own client implementation, but the endpoint is a bean and its handlers are invoked through the compiled bean metadata, so nothing is scanned or reflected on. The handlers may take the container's Session, the EndpointConfig, the Jakarta CloseReason and everything a Micronaut handler can take; a value an @OnMessage method returns is sent to the peer; decoders, encoders and configurator resolve as they do for a @ServerEndpoint.

A class that is not such a bean - one compiled without the processor - is handed to the container as it is, which then instantiates and scans it reflectively, as it would on its own. The programmatic Endpoint forms and the container settings are the container's.

Unlike ContainerProvider.getWebSocketContainer(), whose result depends on which implementation ServiceLoader finds first, this bean is deterministic and is the documented way to open a Jakarta client connection.

Since:
6.2.0
Author:
graemerocher
  • Constructor Details

    • MicronautWebSocketContainer

      public MicronautWebSocketContainer(io.micronaut.context.BeanContext beanContext, ServletWebSocketSupport support, WebSocketContainerHolder holder)
      Default constructor.
      Parameters:
      beanContext - The bean context
      support - The shared infrastructure
      holder - The servlet container's WebSocket container
  • Method Details

    • connectToServer

      public jakarta.websocket.Session connectToServer(Class<?> annotatedEndpointClass, URI path) throws jakarta.websocket.DeploymentException, IOException
      Specified by:
      connectToServer in interface jakarta.websocket.WebSocketContainer
      Throws:
      jakarta.websocket.DeploymentException
      IOException
    • connectToServer

      public jakarta.websocket.Session connectToServer(Object annotatedEndpointInstance, URI path) throws jakarta.websocket.DeploymentException, IOException
      Specified by:
      connectToServer in interface jakarta.websocket.WebSocketContainer
      Throws:
      jakarta.websocket.DeploymentException
      IOException
    • connectToServer

      public jakarta.websocket.Session connectToServer(jakarta.websocket.Endpoint endpointInstance, jakarta.websocket.ClientEndpointConfig cec, URI path) throws jakarta.websocket.DeploymentException, IOException
      Specified by:
      connectToServer in interface jakarta.websocket.WebSocketContainer
      Throws:
      jakarta.websocket.DeploymentException
      IOException
    • connectToServer

      public jakarta.websocket.Session connectToServer(Class<? extends jakarta.websocket.Endpoint> endpointClass, jakarta.websocket.ClientEndpointConfig cec, URI path) throws jakarta.websocket.DeploymentException, IOException

      The endpoint is instantiated without reflection when it can be - as a bean, through its introspection, or reflectively under the reflection policy, the way the components an endpoint declares are. Otherwise the container instantiates it itself, as it would on its own.

      Specified by:
      connectToServer in interface jakarta.websocket.WebSocketContainer
      Throws:
      jakarta.websocket.DeploymentException
      IOException
    • getDefaultAsyncSendTimeout

      public long getDefaultAsyncSendTimeout()
      Specified by:
      getDefaultAsyncSendTimeout in interface jakarta.websocket.WebSocketContainer
    • setAsyncSendTimeout

      public void setAsyncSendTimeout(long timeoutmillis)
      Specified by:
      setAsyncSendTimeout in interface jakarta.websocket.WebSocketContainer
    • getDefaultMaxSessionIdleTimeout

      public long getDefaultMaxSessionIdleTimeout()
      Specified by:
      getDefaultMaxSessionIdleTimeout in interface jakarta.websocket.WebSocketContainer
    • setDefaultMaxSessionIdleTimeout

      public void setDefaultMaxSessionIdleTimeout(long timeout)
      Specified by:
      setDefaultMaxSessionIdleTimeout in interface jakarta.websocket.WebSocketContainer
    • getDefaultMaxBinaryMessageBufferSize

      public int getDefaultMaxBinaryMessageBufferSize()
      Specified by:
      getDefaultMaxBinaryMessageBufferSize in interface jakarta.websocket.WebSocketContainer
    • setDefaultMaxBinaryMessageBufferSize

      public void setDefaultMaxBinaryMessageBufferSize(int max)
      Specified by:
      setDefaultMaxBinaryMessageBufferSize in interface jakarta.websocket.WebSocketContainer
    • getDefaultMaxTextMessageBufferSize

      public int getDefaultMaxTextMessageBufferSize()
      Specified by:
      getDefaultMaxTextMessageBufferSize in interface jakarta.websocket.WebSocketContainer
    • setDefaultMaxTextMessageBufferSize

      public void setDefaultMaxTextMessageBufferSize(int max)
      Specified by:
      setDefaultMaxTextMessageBufferSize in interface jakarta.websocket.WebSocketContainer
    • getInstalledExtensions

      public Set<jakarta.websocket.Extension> getInstalledExtensions()
      Specified by:
      getInstalledExtensions in interface jakarta.websocket.WebSocketContainer