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§
Text
A Text frame
Fields
Binary
A Binary frame
Fields
Close
A Close frame
Ping
A Ping frame
Pong
A Pong frame
Implementations§
Source§impl Frame
impl Frame
Sourcepub fn text(payload: String) -> Self
pub fn text(payload: String) -> Self
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()
.
Sourcepub fn as_text(&self) -> Option<(&String, &bool, &bool)>
pub fn as_text(&self) -> Option<(&String, &bool, &bool)>
Attempts to interpret the frame as a Text frame, returning a reference to the underlying data if it is, and None otherwise.
Sourcepub fn as_text_mut(&mut self) -> Option<(&mut String, &mut bool, &mut bool)>
pub fn as_text_mut(&mut self) -> Option<(&mut String, &mut bool, &mut bool)>
Attempts to interpret the frame as a Text frame, returning a mutable reference to the underlying data if it is, and None otherwise.
Sourcepub fn into_text(self) -> Option<(String, bool, bool)>
pub fn into_text(self) -> Option<(String, bool, bool)>
Attempts to interpret the frame as a Text frame, consuming and returning the underlying data if it is, and returning None otherwise.
Sourcepub fn binary(payload: Vec<u8>) -> Self
pub fn binary(payload: Vec<u8>) -> Self
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()
.
Sourcepub fn as_binary(&self) -> Option<(&Vec<u8>, &bool, &bool)>
pub fn as_binary(&self) -> Option<(&Vec<u8>, &bool, &bool)>
Attempts to interpret the frame as a Binary frame, returning a reference to the underlying data if it is, and None otherwise.
Sourcepub fn as_binary_mut(&mut self) -> Option<(&mut Vec<u8>, &mut bool, &mut bool)>
pub fn as_binary_mut(&mut self) -> Option<(&mut Vec<u8>, &mut bool, &mut bool)>
Attempts to interpret the frame as a Binary frame, returning a mutable reference to the underlying data if it is, and None otherwise.
Sourcepub fn into_binary(self) -> Option<(Vec<u8>, bool, bool)>
pub fn into_binary(self) -> Option<(Vec<u8>, bool, bool)>
Attempts to interpret the frame as a Binary frame, consuming and returning the underlying data if it is, and returning None otherwise.
Sourcepub fn close(payload: Option<(u16, String)>) -> Self
pub fn close(payload: Option<(u16, String)>) -> Self
Constructs a Close frame from the given payload.
Sourcepub fn as_close(&self) -> Option<&(u16, String)>
pub fn as_close(&self) -> Option<&(u16, String)>
Attempts to interpret the frame as a Close frame, returning a reference to the underlying data if it is, and None otherwise.
Sourcepub fn as_close_mut(&mut self) -> Option<&mut (u16, String)>
pub fn as_close_mut(&mut self) -> Option<&mut (u16, String)>
Attempts to interpret the frame as a Close frame, returning a mutable reference to the underlying data if it is, and None otherwise.
Sourcepub fn into_close(self) -> Option<(u16, String)>
pub fn into_close(self) -> Option<(u16, String)>
Attempts to interpret the frame as a Close frame, consuming and returning the underlying data if it is, and returning None otherwise.
Sourcepub fn as_ping(&self) -> Option<&Vec<u8>>
pub fn as_ping(&self) -> Option<&Vec<u8>>
Attempts to interpret the frame as a Ping frame, returning a reference to the underlying data if it is, and None otherwise.
Sourcepub fn as_ping_mut(&mut self) -> Option<&mut Vec<u8>>
pub fn as_ping_mut(&mut self) -> Option<&mut Vec<u8>>
Attempts to interpret the frame as a Ping frame, returning a mutable reference to the underlying data if it is, and None otherwise.
Sourcepub fn into_ping(self) -> Option<Vec<u8>>
pub fn into_ping(self) -> Option<Vec<u8>>
Attempts to interpret the frame as a Ping frame, consuming and returning the underlying data if it is, and returning None otherwise.
Sourcepub fn as_pong(&self) -> Option<&Vec<u8>>
pub fn as_pong(&self) -> Option<&Vec<u8>>
Attempts to interpret the frame as a Pong frame, returning a reference to the underlying data if it is, and None otherwise.
Sourcepub fn as_pong_mut(&mut self) -> Option<&mut Vec<u8>>
pub fn as_pong_mut(&mut self) -> Option<&mut Vec<u8>>
Attempts to interpret the frame as a Pong frame, returning a mutable reference to the underlying data if it is, and None otherwise.
Sourcepub fn into_pong(self) -> Option<Vec<u8>>
pub fn into_pong(self) -> Option<Vec<u8>>
Attempts to interpret the frame as a Pong frame, consuming and returning the underlying data if it is, and returning None otherwise.
Sourcepub fn set_continuation(self, continuation: bool) -> Self
pub fn set_continuation(self, continuation: bool) -> Self
Modifies the frame to set continuation
to the desired value.
If the frame is not a Text or Binary frame, no operation is performed.