pub struct Message { /* private fields */ }
Expand description
A text string, a block of binary data or a WebSocket control frame.
Implementations§
Source§impl Message
impl Message
Sourcepub fn new<B: Into<Bytes>>(opcode: Opcode, data: B) -> Result<Self, Utf8Error>
pub fn new<B: Into<Bytes>>(opcode: Opcode, data: B) -> Result<Self, Utf8Error>
Creates a message from a Bytes
object.
The message can be tagged as text or binary. When the opcode
parameter is Opcode::Text
this function validates the bytes in data
and returns Err
if they do not contain valid UTF-8 text.
Sourcepub fn binary<B: Into<Bytes>>(data: B) -> Self
pub fn binary<B: Into<Bytes>>(data: B) -> Self
Creates a binary message from any type that can be converted to Bytes
, such as &[u8]
or Vec<u8>
.
Sourcepub fn close(reason: Option<(u16, String)>) -> Self
pub fn close(reason: Option<(u16, String)>) -> Self
Creates a message that indicates the connection is about to be closed.
The reason
parameter is an optional numerical status code and text description. Valid reasons
may be defined by a particular WebSocket server.
Sourcepub fn ping<B: Into<Bytes>>(data: B) -> Self
pub fn ping<B: Into<Bytes>>(data: B) -> Self
Creates a message requesting a pong response.
The client can send one of these to request a pong response from the server.
Sourcepub fn pong<B: Into<Bytes>>(data: B) -> Self
pub fn pong<B: Into<Bytes>>(data: B) -> Self
Creates a response to a ping message.
The client can send one of these in response to a ping from the server.
Sourcepub fn as_text(&self) -> Option<&str>
pub fn as_text(&self) -> Option<&str>
For messages with opcode Opcode::Text
, returns a reference to the text.
Returns None
otherwise.