Interface WebSocketSession

  • All Superinterfaces:
    java.lang.AutoCloseable, io.micronaut.core.convert.value.ConvertibleValues<java.lang.Object>, java.lang.Iterable<java.util.Map.Entry<java.lang.String,​java.lang.Object>>, io.micronaut.core.convert.value.MutableConvertibleValues<java.lang.Object>, io.micronaut.core.value.ValueResolver<java.lang.CharSequence>

    public interface WebSocketSession
    extends io.micronaut.core.convert.value.MutableConvertibleValues<java.lang.Object>, java.lang.AutoCloseable
    Represents an open WebSocket connection. Based largely on javax.websocket and likely to be able to implement the spec in the future.
    Since:
    1.0
    • Field Summary

      • Fields inherited from interface io.micronaut.core.convert.value.ConvertibleValues

        EMPTY
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      void close()  
      void close​(CloseReason closeReason)
      Close the session with the given event.
      io.micronaut.core.convert.value.MutableConvertibleValues<java.lang.Object> getAttributes()  
      java.lang.String getId()
      The ID of the session.
      java.util.Set<? extends WebSocketSession> getOpenSessions()
      The current open sessions.
      java.lang.String getProtocolVersion()
      The protocol version of the WebSocket protocol currently being used.
      default io.micronaut.core.convert.value.ConvertibleMultiValues<java.lang.String> getRequestParameters()
      The request parameters used to create this session.
      java.net.URI getRequestURI()
      The request URI this session was opened under.
      default java.util.Optional<java.lang.String> getSubprotocol()
      The subprotocol if one is used.
      default io.micronaut.core.convert.value.ConvertibleValues<java.lang.Object> getUriVariables()
      Any matching URI path variables.
      default java.util.Optional<java.security.Principal> getUserPrincipal()
      The user Principal used to create the session.
      boolean isOpen()
      Whether the session is open.
      boolean isSecure()
      Whether the connection is secure.
      boolean isWritable()
      Whether the session is writable.
      default <T> org.reactivestreams.Publisher<T> send​(T message)
      Send the given message to the remote peer.
      <T> org.reactivestreams.Publisher<T> send​(T message, io.micronaut.http.MediaType mediaType)
      Send the given message to the remote peer.
      default <T> java.util.concurrent.CompletableFuture<T> sendAsync​(T message)
      Send the given message to the remote peer asynchronously.
      <T> java.util.concurrent.CompletableFuture<T> sendAsync​(T message, io.micronaut.http.MediaType mediaType)
      Send the given message to the remote peer asynchronously.
      default java.util.concurrent.CompletableFuture<?> sendPingAsync​(byte[] content)
      Send a ping through this WebSocket.
      default void sendSync​(java.lang.Object message)
      Send the given message to the remote peer synchronously.
      default void sendSync​(java.lang.Object message, io.micronaut.http.MediaType mediaType)
      Send the given message to the remote peer synchronously.
      • Methods inherited from interface io.micronaut.core.convert.value.ConvertibleValues

        asMap, asMap, asProperties, contains, forEach, getValue, getValueType, isEmpty, iterator, names, subMap, subMap, subMap, values
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
      • Methods inherited from interface io.micronaut.core.convert.value.MutableConvertibleValues

        clear, put, putAll, putAll, remove
      • Methods inherited from interface io.micronaut.core.value.ValueResolver

        get, get, get, get
    • Method Detail

      • getId

        java.lang.String getId()
        The ID of the session.
        Returns:
        The ID of the session
      • getAttributes

        io.micronaut.core.convert.value.MutableConvertibleValues<java.lang.Object> getAttributes()
        Returns:
        Only the attributes of the session
      • isOpen

        boolean isOpen()
        Whether the session is open.
        Returns:
        True if it is
      • isWritable

        boolean isWritable()
        Whether the session is writable. It may not be writable, if the buffer is currently full
        Returns:
        True if it is
      • isSecure

        boolean isSecure()
        Whether the connection is secure.
        Returns:
        True if it is secure
      • getOpenSessions

        java.util.Set<? extends WebSocketSession> getOpenSessions()
        The current open sessions.
        Returns:
        The open sessions
      • getRequestURI

        java.net.URI getRequestURI()
        The request URI this session was opened under.
        Returns:
        The request URI
      • getProtocolVersion

        java.lang.String getProtocolVersion()
        The protocol version of the WebSocket protocol currently being used.
        Returns:
        The protocol version
      • send

        <T> org.reactivestreams.Publisher<T> send​(T message,
                                                  io.micronaut.http.MediaType mediaType)
        Send the given message to the remote peer. The resulting Publisher does not start sending until subscribed to. If you return it from Micronaut annotated methods such as OnOpen and OnMessage, Micronaut will subscribe to it and send the message without blocking.
        Type Parameters:
        T - The message type
        Parameters:
        message - The message
        mediaType - The media type of the message. Used to lookup an appropriate codec via the MediaTypeCodecRegistry.
        Returns:
        A Publisher that either emits an error or emits the message once it has been published successfully.
      • sendAsync

        <T> java.util.concurrent.CompletableFuture<T> sendAsync​(T message,
                                                                io.micronaut.http.MediaType mediaType)
        Send the given message to the remote peer asynchronously.
        Type Parameters:
        T - The message type
        Parameters:
        message - The message
        mediaType - The media type of the message. Used to lookup an appropriate codec via the MediaTypeCodecRegistry.
        Returns:
        A CompletableFuture that tracks the execution. CompletableFuture.get() and related methods will return the message on success, on error throw the underlying Exception.
      • sendSync

        default void sendSync​(java.lang.Object message,
                              io.micronaut.http.MediaType mediaType)
        Send the given message to the remote peer synchronously.
        Parameters:
        message - The message
        mediaType - The media type of the message. Used to lookup an appropriate codec via the MediaTypeCodecRegistry.
      • send

        default <T> org.reactivestreams.Publisher<T> send​(T message)
        Send the given message to the remote peer. The resulting Publisher does not start sending until subscribed to. If you return it from Micronaut annotated methods such as OnOpen and OnMessage, Micronaut will subscribe to it and send the message without blocking.
        Type Parameters:
        T - The message type
        Parameters:
        message - The message
        Returns:
        A Publisher that either emits an error or emits the message once it has been published successfully.
      • sendAsync

        default <T> java.util.concurrent.CompletableFuture<T> sendAsync​(T message)
        Send the given message to the remote peer asynchronously.
        Type Parameters:
        T - The message type
        Parameters:
        message - The message
        Returns:
        A CompletableFuture that tracks the execution. CompletableFuture.get() and related methods will return the message on success, on error throw the underlying Exception.
      • sendSync

        default void sendSync​(java.lang.Object message)
        Send the given message to the remote peer synchronously.
        Parameters:
        message - The message
      • sendPingAsync

        @NonNull
        default java.util.concurrent.CompletableFuture<?> sendPingAsync​(@NonNull
                                                                        byte[] content)
        Send a ping through this WebSocket. The pong reply can be intercepted using a @OnMessage method that accepts a WebSocketPongMessage.
        Parameters:
        content - The content of the ping. The remote should return the same content in its WebSocketPongMessage.
        Returns:
        A future that completes when the ping has been sent. (Not when the pong has been received!)
      • getSubprotocol

        default java.util.Optional<java.lang.String> getSubprotocol()
        The subprotocol if one is used.
        Returns:
        The subprotocol
      • getRequestParameters

        default io.micronaut.core.convert.value.ConvertibleMultiValues<java.lang.String> getRequestParameters()
        The request parameters used to create this session.
        Returns:
        The request parameters
      • getUriVariables

        default io.micronaut.core.convert.value.ConvertibleValues<java.lang.Object> getUriVariables()
        Any matching URI path variables.
        Returns:
        The path variables
      • getUserPrincipal

        default java.util.Optional<java.security.Principal> getUserPrincipal()
        The user Principal used to create the session.
        Returns:
        The Principal
      • close

        void close()
        Specified by:
        close in interface java.lang.AutoCloseable
      • close

        void close​(CloseReason closeReason)
        Close the session with the given event.
        Parameters:
        closeReason - The close event