Enum websockets::Frame [−][src]
pub enum Frame { Text { payload: String, continuation: bool, fin: bool, }, Binary { payload: Vec<u8>, continuation: bool, fin: bool, }, Close { payload: Option<(u16, String)>, }, Ping { payload: Option<Vec<u8>>, }, Pong { payload: Option<Vec<u8>>, }, }
Expand description
Data which is sent and received through the WebSocket connection.
Sending
To send a Frame, you can construct it normally and use the WebSocket::send()
method,
or use the convenience methods for each frame type
(send_text()
, send_binary()
,
close()
, send_ping()
,
and send_pong()
).
Receiving
Frames can be received through the WebSocket::receive()
method.
To extract the underlying data from a received Frame,
you can match
or use the convenience methods—for example, for text frames,
you can use the method as_text
to get an immutable reference
to the data, as_text_mut
to get a mutable reference to the data,
or into_text
to get ownership of the data.
Fragmentation
As per the WebSocket protocol, frames can actually be fragments in a larger message
(see https://tools.ietf.org/html/rfc6455#section-5.4).
However, the maximum frame size allowed by the WebSocket protocol is larger
than what can be stored in a Vec
. Therefore, no strategy for splitting messages
into Frames is provided by this library.
If you would like to use fragmentation manually, this can be done by setting
the continuation
and fin
flags on the Text
and Binary
variants.
continuation
signifies that the Frame is a Continuation frame in the message,
and fin
signifies that the Frame is the final frame in the message
(see the above linked RFC for more details).
For example, if the message contains only one Frame, the single frame
should have continuation
set to false
and fin
set to true
. If the message
contains more than one frame, the first frame should have continuation
set to
false
and fin
set to false
, all other frames except the last frame should
have continuation
set to true
and fin
set to false
, and the last frame should
have continuation
set to true
and fin
set to true
.
Variants
A Text frame
Fields of Text
A Binary frame
Fields of Binary
A Close frame
A Ping frame
A Pong frame
Implementations
Constructs a Text frame from the given payload.
continuation
will be false
and fin
will be true
.
This can be modified by chaining Frame::set_continuation()
or Frame::set_fin()
.
Attempts to interpret the frame as a Text frame, returning a reference to the underlying data if it is, and None otherwise.
Attempts to interpret the frame as a Text frame, returning a mutable reference to the underlying data if it is, and None otherwise.
Attempts to interpret the frame as a Text frame, consuming and returning the underlying data if it is, and returning None otherwise.
Constructs a Binary frame from the given payload.
continuation
will be false
and fin
will be true
.
This can be modified by chaining Frame::set_continuation()
or Frame::set_fin()
.
Attempts to interpret the frame as a Binary frame, returning a reference to the underlying data if it is, and None otherwise.
Attempts to interpret the frame as a Binary frame, returning a mutable reference to the underlying data if it is, and None otherwise.
Attempts to interpret the frame as a Binary frame, consuming and returning the underlying data if it is, and returning None otherwise.
Constructs a Close frame from the given payload.
Attempts to interpret the frame as a Close frame, returning a reference to the underlying data if it is, and None otherwise.
Attempts to interpret the frame as a Close frame, returning a mutable reference to the underlying data if it is, and None otherwise.
Attempts to interpret the frame as a Close frame, consuming and returning the underlying data if it is, and returning None otherwise.
Attempts to interpret the frame as a Ping frame, returning a reference to the underlying data if it is, and None otherwise.
Attempts to interpret the frame as a Ping frame, returning a mutable reference to the underlying data if it is, and None otherwise.
Attempts to interpret the frame as a Ping frame, consuming and returning the underlying data if it is, and returning None otherwise.
Attempts to interpret the frame as a Pong frame, returning a reference to the underlying data if it is, and None otherwise.
Attempts to interpret the frame as a Pong frame, returning a mutable reference to the underlying data if it is, and None otherwise.
Attempts to interpret the frame as a Pong frame, consuming and returning the underlying data if it is, and returning None otherwise.
Modifies the frame to set continuation
to the desired value.
If the frame is not a Text or Binary frame, no operation is performed.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Frame
impl UnwindSafe for Frame
Blanket Implementations
Mutably borrows from an owned value. Read more
type Output = T
type Output = T
Should always be Self