pub struct Message { /* private fields */ }
Expand description
A WebSocket message.
Implementations§
Source§impl Message
impl Message
Sourcepub fn text<S: Into<String>>(s: S) -> Message
pub fn text<S: Into<String>>(s: S) -> Message
Create a new Text
WebSocket message from a stringable.
Sourcepub fn json<T: Serialize>(value: &T) -> Result<Message>
pub fn json<T: Serialize>(value: &T) -> Result<Message>
Constructs a Text
WebSocket message with the json value.
§Optional
This requires the optional json
feature to be enabled.
Sourcepub fn ping<V: Into<Vec<u8>>>(v: V) -> Message
pub fn ping<V: Into<Vec<u8>>>(v: V) -> Message
Construct a new Ping
WebSocket message.
The payload here must have a length less than 125 bytes.
Sourcepub fn pong<V: Into<Vec<u8>>>(v: V) -> Message
pub fn pong<V: Into<Vec<u8>>>(v: V) -> Message
Construct a new Pong
WebSocket message.
The payload here must have a length less than 125 bytes.
Sourcepub fn close_with<R: Into<Cow<'static, str>>>(
code: CloseCode,
reason: R,
) -> Message
pub fn close_with<R: Into<Cow<'static, str>>>( code: CloseCode, reason: R, ) -> Message
Construct a Close
WebSocket message with a code and reason.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the WebSocket message has no content. For example, if the other side of the connection sent an empty string.
Sourcepub fn close_code(&self) -> Option<CloseCode>
pub fn close_code(&self) -> Option<CloseCode>
The Close
code if available.
Sourcepub fn close_reason(&self) -> Option<&str>
pub fn close_reason(&self) -> Option<&str>
The Close
reason if available.
Sourcepub fn as_text(&self) -> Result<&str>
pub fn as_text(&self) -> Result<&str>
Attempts to convert the message data as text in UTF8
format.
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Consumes the message and returns its data as bytes.
Sourcepub fn into_text(self) -> Result<String>
pub fn into_text(self) -> Result<String>
Consumes the WebSocket message and attempts to converts it to a String
.
Sourcepub fn decode_json<T: DeserializeOwned>(self) -> Result<T>
pub fn decode_json<T: DeserializeOwned>(self) -> Result<T>
Try to deserialize the message data as JSON
.
§Optional
This requires the optional json
feature to be enabled.
Trait Implementations§
Source§impl Sink<Message> for WebSocket
impl Sink<Message> for WebSocket
Source§type Error = WebsocketError
type Error = WebsocketError
Source§fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>
fn poll_ready( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>
Sink
to receive a value. Read moreSource§fn start_send(self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error>
fn start_send(self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error>
poll_ready
which returned Poll::Ready(Ok(()))
. Read more