pub enum Message {
Text(String),
Binary(Bytes),
Ping(Bytes),
Pong(Bytes),
Close {
code: CloseCode,
reason: String,
},
}Expand description
A WebSocket message, which can be a text string or binary data.
Variants§
Text(String)
A text WebSocket message.
Binary(Bytes)
A binary WebSocket message.
Ping(Bytes)
A ping message with the specified payload.
The payload here must have a length less than 125 bytes.
§WASM
This variant is ignored for WASM targets.
Pong(Bytes)
A pong message with the specified payload.
The payload here must have a length less than 125 bytes.
§WASM
This variant is ignored for WASM targets.
Close
A close message.
Sending this will not close the connection. Use WebSocket::close for this.
Though the remote peer will likely close the connection after receiving this.
Implementations§
Source§impl Message
impl Message
Sourcepub fn text_from_json<T: Serialize + ?Sized>(json: &T) -> Result<Self, Error>
Available on crate feature json only.
pub fn text_from_json<T: Serialize + ?Sized>(json: &T) -> Result<Self, Error>
json only.Tries to serialize the JSON as a Message::Text.
§Optional
This requires the optional json feature enabled.
§Errors
Serialization can fail if T’s implementation of Serialize decides to
fail, or if T contains a map with non-string keys.
Sourcepub fn binary_from_json<T: Serialize + ?Sized>(json: &T) -> Result<Self, Error>
Available on crate feature json only.
pub fn binary_from_json<T: Serialize + ?Sized>(json: &T) -> Result<Self, Error>
json only.Tries to serialize the JSON as a Message::Binary.
§Optional
This requires that the optional json feature is enabled.
§Errors
Serialization can fail if T’s implementation of Serialize decides to
fail, or if T contains a map with non-string keys.
Sourcepub fn json<T: DeserializeOwned>(&self) -> Result<T, Error>
Available on crate feature json only.
pub fn json<T: DeserializeOwned>(&self) -> Result<T, Error>
json only.Tries to deserialize the message body as JSON.
§Optional
This requires that the optional json feature is enabled.
§Errors
This method fails whenever the response body is not in JSON format,
or it cannot be properly deserialized to target type T.
For more details please see serde_json::from_str and
serde_json::from_slice.
Trait Implementations§
Source§impl Sink<Message> for WebSocket
impl Sink<Message> for WebSocket
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