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>
impl<'a> Message<'a>
Sourcepub fn binary<B>(data: B) -> Selfwhere
B: IntoCowBytes<'a>,
pub fn binary<B>(data: B) -> Selfwhere
B: IntoCowBytes<'a>,
Create a new WebSocket message with binary data
Sourcepub fn close() -> Self
pub fn close() -> Self
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) -> Self
pub fn close_because<S>(code: u16, reason: S) -> Self
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) -> Selfwhere
P: IntoCowBytes<'a>,
pub fn ping<P>(data: P) -> Selfwhere
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) -> Selfwhere
P: IntoCowBytes<'a>,
pub fn pong<P>(data: P) -> Selfwhere
P: IntoCowBytes<'a>,
Create a pong WebSocket message, usually a response to a ping message
Trait Implementations§
Source§impl<'a> DataFrame for Message<'a>
impl<'a> DataFrame for Message<'a>
Source§fn write_payload(&self, socket: &mut dyn Write) -> WebSocketResult<()>
fn write_payload(&self, socket: &mut dyn Write) -> WebSocketResult<()>
Source§fn take_payload(self) -> Vec<u8>
fn take_payload(self) -> Vec<u8>
Source§fn frame_size(&self, masked: bool) -> usize
fn frame_size(&self, masked: bool) -> usize
Source§impl<'m> From<Message<'m>> for OwnedMessage
impl<'m> From<Message<'m>> for OwnedMessage
Source§impl<'m> From<OwnedMessage> for Message<'m>
impl<'m> From<OwnedMessage> for Message<'m>
Source§fn from(message: OwnedMessage) -> Self
fn from(message: OwnedMessage) -> Self
Source§impl<'a> Message for Message<'a>
impl<'a> Message for Message<'a>
Source§fn serialize(&self, writer: &mut dyn Write, masked: bool) -> WebSocketResult<()>
fn serialize(&self, writer: &mut dyn Write, masked: bool) -> WebSocketResult<()>
Attempt to form a message from a series of data frames
Source§fn message_size(&self, masked: bool) -> usize
fn message_size(&self, masked: bool) -> usize
Returns how many bytes this message will take up
Source§fn from_dataframes<D>(frames: Vec<D>) -> WebSocketResult<Self>where
D: DataFrameTrait,
fn from_dataframes<D>(frames: Vec<D>) -> WebSocketResult<Self>where
D: DataFrameTrait,
Attempt to form a message from a series of data frames