Struct websocket::message::Message

source ·
pub struct Message<'a> {
    pub opcode: Type,
    pub cd_status_code: Option<u16>,
    pub payload: Cow<'a, [u8]>,
}
Expand description

Represents a WebSocket message.

This message also has the ability to not own its payload, and stores its entire payload in chunks that get written in order when the message gets sent. This makes the write_payload allocate less memory than the payload method (which creates a new buffer every time).

Incidentally this (the default implementation of Message) implements the DataFrame trait because this message just gets sent as one single DataFrame.

Fields§

§opcode: Type

Type of WebSocket message

§cd_status_code: Option<u16>

Optional status code to send when closing a connection. (only used if this message is of Type::Close)

§payload: Cow<'a, [u8]>

Main payload

Implementations§

source§

impl<'a> Message<'a>

source

pub fn text<S>(data: S) -> Message<'a>
where S: Into<Cow<'a, str>>,

Create a new WebSocket message with text data

source

pub fn binary<B>(data: B) -> Message<'a>
where B: IntoCowBytes<'a>,

Create a new WebSocket message with binary data

source

pub fn close() -> Message<'a>

Create a new WebSocket message that signals the end of a WebSocket connection, although messages can still be sent after sending this

source

pub fn close_because<S>(code: u16, reason: S) -> Message<'a>
where S: Into<Cow<'a, str>>,

Create a new WebSocket message that signals the end of a WebSocket connection and provide a text reason and a status code for why. Messages can still be sent after sending this message.

source

pub fn ping<P>(data: P) -> Message<'a>
where P: IntoCowBytes<'a>,

Create a ping WebSocket message, a pong is usually sent back after sending this with the same data

source

pub fn pong<P>(data: P) -> Message<'a>
where P: IntoCowBytes<'a>,

Create a pong WebSocket message, usually a response to a ping message

source

pub fn into_pong(&mut self) -> Result<(), ()>

Convert a ping message to a pong, keeping the data. This will fail if the original message is not a ping.

Trait Implementations§

source§

impl<'a> Clone for Message<'a>

source§

fn clone(&self) -> Message<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> DataFrame for Message<'a>

source§

fn is_last(&self) -> bool

Is this dataframe the final dataframe of the message?
source§

fn opcode(&self) -> u8

What type of data does this dataframe contain?
source§

fn reserved(&self) -> &[bool; 3]

Reserved bits of this dataframe
source§

fn size(&self) -> usize

How long (in bytes) is this dataframe’s payload
source§

fn write_payload(&self, socket: &mut dyn Write) -> Result<(), WebSocketError>

Write the payload to a writer
source§

fn take_payload(self) -> Vec<u8>

Takes the payload out into a vec
source§

fn frame_size(&self, masked: bool) -> usize

Get’s the size of the entire dataframe in bytes, i.e. header and payload.
source§

fn write_to( &self, writer: &mut dyn Write, mask: bool ) -> Result<(), WebSocketError>

Writes a DataFrame to a Writer.
source§

impl<'a> Debug for Message<'a>

source§

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

Formats the value using the given formatter. Read more
source§

impl<'m> From<Message<'m>> for OwnedMessage

source§

fn from(message: Message<'m>) -> OwnedMessage

Converts to this type from the input type.
source§

impl<'m> From<OwnedMessage> for Message<'m>

source§

fn from(message: OwnedMessage) -> Message<'m>

Converts to this type from the input type.
source§

impl<'a> Message for Message<'a>

source§

fn serialize( &self, writer: &mut dyn Write, masked: bool ) -> Result<(), WebSocketError>

Attempt to form a message from a series of data frames

source§

fn message_size(&self, masked: bool) -> usize

Returns how many bytes this message will take up

source§

fn from_dataframes<D>(frames: Vec<D>) -> Result<Message<'a>, WebSocketError>
where D: DataFrame,

Attempt to form a message from a series of data frames

source§

impl<'a> PartialEq for Message<'a>

source§

fn eq(&self, other: &Message<'a>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> Eq for Message<'a>

source§

impl<'a> StructuralPartialEq for Message<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Message<'a>

§

impl<'a> RefUnwindSafe for Message<'a>

§

impl<'a> Send for Message<'a>

§

impl<'a> Sync for Message<'a>

§

impl<'a> Unpin for Message<'a>

§

impl<'a> UnwindSafe for Message<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T> Typeable for T
where T: Any,

source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.