pub enum Message {
Text(Utf8Bytes),
Binary(Bytes),
Ping(Bytes),
Pong(Bytes),
Close(Option<CloseFrame>),
}
Expand description
A WebSocket message.
Variants§
Text(Utf8Bytes)
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.
Ping messages will be automatically responded to by the server, so you do not have to worry about dealing with them yourself.
Pong(Bytes)
A pong message with the specified payload
The payload here must have a length less than 125 bytes.
Pong messages will be automatically sent to the client if a ping message is received, so you do not have to worry about constructing them yourself unless you want to implement a unidirectional heartbeat.
Close(Option<CloseFrame>)
A close message with the optional close frame.
You may “uncleanly” close a WebSocket connection at any time
by simply dropping the WebSocket
.
However, you may also use the graceful closing protocol, in which
- peer A sends a close frame, and does not send any further messages;
- peer B responds with a close frame, and does not send any further messages;
- peer A processes the remaining messages sent by peer B, before finally
- both peers close the connection.
After sending a close frame, you may still read messages, but attempts to send another message will error. After receiving a close frame, axum will automatically respond with a close frame if necessary (you do not have to deal with this yourself). Since no further messages will be received, you may either do nothing or explicitly drop the connection.
Implementations§
Source§impl Message
impl Message
Sourcepub fn into_text(self) -> Result<Utf8Bytes, Error>
pub fn into_text(self) -> Result<Utf8Bytes, Error>
Attempt to consume the WebSocket message and convert it to a Utf8Bytes.
Sourcepub fn to_text(&self) -> Result<&str, Error>
pub fn to_text(&self) -> Result<&str, Error>
Attempt to get a &str from the WebSocket message, this will try to convert binary data to utf8.
Trait Implementations§
Source§impl Sink<Message> for WebSocket
impl Sink<Message> for WebSocket
Source§fn poll_ready(
self: Pin<&mut WebSocket>,
cx: &mut Context<'_>,
) -> Poll<Result<(), <WebSocket as Sink<Message>>::Error>>
fn poll_ready( self: Pin<&mut WebSocket>, cx: &mut Context<'_>, ) -> Poll<Result<(), <WebSocket as Sink<Message>>::Error>>
Sink
to receive a value. Read moreSource§fn start_send(
self: Pin<&mut WebSocket>,
item: Message,
) -> Result<(), <WebSocket as Sink<Message>>::Error>
fn start_send( self: Pin<&mut WebSocket>, item: Message, ) -> Result<(), <WebSocket as Sink<Message>>::Error>
poll_ready
which returned Poll::Ready(Ok(()))
. Read more