Enum Message

enum Message

A WebSocket message.

Variants

Text(Utf8Bytes)

A text WebSocket message

Binary(crate::body::Bytes)

A binary WebSocket message

Ping(crate::body::Bytes)

A ping message with the specified payload

The payload here must have a length less than 125 bytes.

Ping messages will be automatically responded to by the server, so you do not have to worry about dealing with them yourself.

Pong(crate::body::Bytes)

A pong message with the specified payload

The payload here must have a length less than 125 bytes.

Pong messages will be automatically sent to the client if a ping message is received, so you do not have to worry about constructing them yourself unless you want to implement a unidirectional heartbeat.

Close(Option<CloseFrame>)

A close message with the optional close frame.

You may "uncleanly" close a WebSocket connection at any time by simply dropping the WebSocket. However, you may also use the graceful closing protocol, in which

  1. peer A sends a close frame, and does not send any further messages;
  2. peer B responds with a close frame, and does not send any further messages;
  3. peer A processes the remaining messages sent by peer B, before finally
  4. both peers close the connection.

After sending a close frame, you may still read messages, but attempts to send another message will error. After receiving a close frame, axum will automatically respond with a close frame if necessary (you do not have to deal with this yourself). Since no further messages will be received, you may either do nothing or explicitly drop the connection.

Implementations

impl Message

fn into_data(self: Self) -> Bytes

Consume the WebSocket and return it as binary data.

fn into_text(self: Self) -> Result<Utf8Bytes, Error>

Attempt to consume the WebSocket message and convert it to a Utf8Bytes.

fn to_text(self: &Self) -> Result<&str, Error>

Attempt to get a &str from the WebSocket message, this will try to convert binary data to utf8.

fn text<S>(string: S) -> Message
where
    S: Into<Utf8Bytes>

Create a new text WebSocket message from a stringable.

fn binary<B>(bin: B) -> Message
where
    B: Into<Bytes>

Create a new binary WebSocket message by converting to Bytes.

impl Clone for Message

fn clone(self: &Self) -> Message

impl Debug for Message

fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result

impl Eq for Message

impl Freeze for Message

impl From for Message

fn from(data: Bytes) -> Self

impl From for Message

fn from(data: Vec<u8>) -> Self

impl From for Message

fn from(string: String) -> Self

impl PartialEq for Message

fn eq(self: &Self, other: &Message) -> bool

impl RefUnwindSafe for Message

impl Send for Message

impl StructuralPartialEq for Message

impl Sync for Message

impl Unpin for Message

impl UnwindSafe for Message

impl<'b> From for Message

fn from(data: &'b [u8]) -> Self

impl<'s> From for Message

fn from(string: &'s str) -> Self

impl<Q, K> Equivalent for Message

fn equivalent(self: &Self, key: &K) -> bool

impl<Q, K> Equivalent for Message

fn equivalent(self: &Self, key: &K) -> bool

impl<T> Any for Message

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Message

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Message

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for Message

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Message

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> FromRef for Message

fn from_ref(input: &T) -> T

impl<T> Instrument for Message

impl<T> Same for Message

impl<T> ToOwned for Message

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> WithSubscriber for Message

impl<T, U> Into for Message

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Message

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Message

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>

impl<V, T> VZip for Message

fn vzip(self: Self) -> V