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
sourceimpl<'a> Message<'a>
impl<'a> Message<'a>
sourcepub fn text<S>(data: S) -> Message<'a> where
S: Into<Cow<'a, str>>,
pub fn text<S>(data: S) -> Message<'a> where
S: Into<Cow<'a, str>>,
Create a new WebSocket message with text data
sourcepub fn binary<B>(data: B) -> Message<'a> where
B: IntoCowBytes<'a>,
pub fn binary<B>(data: B) -> Message<'a> where
B: IntoCowBytes<'a>,
Create a new WebSocket message with binary data
sourcepub fn close() -> Message<'a>
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
sourcepub fn close_because<S>(code: u16, reason: S) -> Message<'a> where
S: Into<Cow<'a, str>>,
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.
sourcepub fn ping<P>(data: P) -> Message<'a> where
P: IntoCowBytes<'a>,
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
sourcepub fn pong<P>(data: P) -> Message<'a> where
P: IntoCowBytes<'a>,
pub fn pong<P>(data: P) -> Message<'a> where
P: IntoCowBytes<'a>,
Create a pong WebSocket message, usually a response to a ping message
Trait Implementations
sourceimpl<'a> DataFrame for Message<'a>
impl<'a> DataFrame for Message<'a>
sourcefn write_payload(&self, socket: &mut dyn Write) -> Result<(), WebSocketError>
fn write_payload(&self, socket: &mut dyn Write) -> Result<(), WebSocketError>
Write the payload to a writer
sourcefn take_payload(self) -> Vec<u8, Global>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
fn take_payload(self) -> Vec<u8, Global>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Takes the payload out into a vec
sourcefn frame_size(&self, masked: bool) -> usize
fn frame_size(&self, masked: bool) -> usize
Get’s the size of the entire dataframe in bytes, i.e. header and payload. Read more
sourceimpl<'m> From<Message<'m>> for OwnedMessage
impl<'m> From<Message<'m>> for OwnedMessage
sourcefn from(message: Message<'m>) -> OwnedMessage
fn from(message: Message<'m>) -> OwnedMessage
Converts to this type from the input type.
sourceimpl<'m> From<OwnedMessage> for Message<'m>
impl<'m> From<OwnedMessage> for Message<'m>
sourcefn from(message: OwnedMessage) -> Message<'m>
fn from(message: OwnedMessage) -> Message<'m>
Converts to this type from the input type.
sourceimpl<'a> Message for Message<'a>
impl<'a> Message for Message<'a>
sourcefn serialize(
&self,
writer: &mut dyn Write,
masked: bool
) -> Result<(), WebSocketError>
fn serialize(
&self,
writer: &mut dyn Write,
masked: bool
) -> Result<(), WebSocketError>
Attempt to form a message from a series of data frames
sourcefn message_size(&self, masked: bool) -> usize
fn message_size(&self, masked: bool) -> usize
Returns how many bytes this message will take up
sourcefn from_dataframes<D>(
frames: Vec<D, Global>
) -> Result<Message<'a>, WebSocketError> where
D: DataFrame,
fn from_dataframes<D>(
frames: Vec<D, Global>
) -> Result<Message<'a>, WebSocketError> where
D: DataFrame,
Attempt to form a message from a series of data frames
impl<'a> Eq for Message<'a>
impl<'a> StructuralEq for Message<'a>
impl<'a> StructuralPartialEq for Message<'a>
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more