org.springframework.web.reactive.socket

## Class WebSocketMessage

- java.lang.Object
- - org.springframework.web.reactive.socket.WebSocketMessage

-     public class WebSocketMessage
      extends Object

  Representation of a WebSocket message.

  See static factory methods in `WebSocketSession` for creating messages with the `DataBufferFactory` for the session.

  Since:
    5.0
  Author:
    Rossen Stoyanchev

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `WebSocketMessage.Type` WebSocket message types.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `WebSocketMessage(WebSocketMessage.Type type, DataBuffer payload)` Constructor for a WebSocketMessage.
    `WebSocketMessage(WebSocketMessage.Type type, DataBuffer payload, Object nativeMessage)` Constructor for an inbound message with access to the underlying message.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `equals(Object other)`
    `<T> T` | `getNativeMessage()` Return the message from the API of the underlying WebSocket library.
    `DataBuffer` | `getPayload()` Return the message payload.
    `String` | `getPayloadAsText()` A variant of `getPayloadAsText(Charset)` that uses `UTF-8` for decoding the raw content to text.
    `String` | `getPayloadAsText(Charset charset)` A shortcut for decoding the raw content of the message to text with the given character encoding.
    `WebSocketMessage.Type` | `getType()` Return the message type (text, binary, etc).
    `int` | `hashCode()`
    `void` | `release()` Release the payload `DataBuffer` which is useful on runtimes (e.g.
    `WebSocketMessage` | `retain()` Retain the data buffer for the message payload, which is useful on runtimes (e.g.
    `String` | `toString()`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### WebSocketMessage

          public WebSocketMessage(WebSocketMessage.Type type,
                                  DataBuffer payload)

      Constructor for a WebSocketMessage.

      See static factory methods in `WebSocketSession` or alternatively use `WebSocketSession.bufferFactory()` to create the payload and then invoke this constructor.

    - #### WebSocketMessage

          public WebSocketMessage(WebSocketMessage.Type type,
                                  DataBuffer payload,
                                  @Nullable
                                  Object nativeMessage)

      Constructor for an inbound message with access to the underlying message.
      Parameters:
        `type` - the type of WebSocket message
        `payload` - the message content
        `nativeMessage` - the message from the API of the underlying WebSocket library, if applicable.
      Since:
        5.3

  - ### Method Detail

    - #### getType

          public WebSocketMessage.Type getType()

      Return the message type (text, binary, etc).

    - #### getPayload

          public DataBuffer getPayload()

      Return the message payload.

    - #### getNativeMessage

          @Nullable
          public <T> T getNativeMessage()

      Return the message from the API of the underlying WebSocket library. This is applicable for inbound messages only and when the underlying message has additional fields other than the content. Currently this is the case for Reactor Netty only.
      Type Parameters:
        `T` - the type to cast the underlying message to
      Returns:
        the underlying message, or `null`
      Since:
        5.3

    - #### getPayloadAsText

          public String getPayloadAsText()

      A variant of `getPayloadAsText(Charset)` that uses `UTF-8` for decoding the raw content to text.

    - #### getPayloadAsText

          public String getPayloadAsText(Charset charset)

      A shortcut for decoding the raw content of the message to text with the given character encoding. This is useful for text WebSocket messages, or otherwise when the payload is expected to contain text.
      Parameters:
        `charset` - the character encoding
      Since:
        5.0.5

    - #### retain

          public WebSocketMessage retain()

      Retain the data buffer for the message payload, which is useful on runtimes (e.g. Netty) with pooled buffers. A shortcut for:

           DataBuffer payload = message.getPayload();
           DataBufferUtils.retain(payload);

      See Also:
        `DataBufferUtils.retain(DataBuffer)`

    - #### release

          public void release()

      Release the payload `DataBuffer` which is useful on runtimes (e.g. Netty) with pooled buffers such as Netty. A shortcut for:

           DataBuffer payload = message.getPayload();
           DataBufferUtils.release(payload);

      See Also:
        `DataBufferUtils.release(DataBuffer)`

    - #### equals

          public boolean equals(@Nullable
                                Object other)

      Overrides:
        `equals` in class `Object`

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `Object`

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`