[−][src]Struct websockets::WebSocket
Manages the WebSocket connection; used to connect, send data, and receive data.
Connect directly with WebSocket::connect()
...
let mut ws = WebSocket::connect("wss://echo.websocket.org/") .await .unwrap();
...or customize the handshake using a WebSocketBuilder
obtained from WebSocket::builder()
.
let mut ws = WebSocket::builder() .add_subprotocol("wamp") .connect("wss://echo.websocket.org") .await .unwrap();
Use the WebSocket::send*
methods to send frames...
ws.send_text("foo".to_string(), false, true).await.unwrap();
...and WebSocket::receive()
to receive frames.
let received_frame = ws.receive().await.unwrap(); let received_msg = received_frame.as_text().unwrap().0.clone(); assert_eq!(received_msg, "foo".to_string()); // echo.websocket.org echoes text frames
Finally, close the connection with WebSocket::close()
.
let status_code = ws.close(Some((1000, String::new()))) .await .unwrap() .as_close() .unwrap() .0; assert_eq!(status_code, 1000);
Implementations
impl WebSocket
[src]
pub fn builder() -> WebSocketBuilder
[src]
Constructs a WebSocketBuilder
, which can be used to customize
the WebSocket handshake.
pub async fn connect<'_>(url: &'_ str) -> Result<Self, WebSocketError>
[src]
Connects to a URL (and performs the WebSocket handshake).
pub async fn send<'_>(&'_ mut self, frame: Frame) -> Result<(), WebSocketError>
[src]
Sends an already constructed Frame
over the WebSocket connection.
pub fn ready_to_receive(&mut self) -> Result<bool, WebSocketError>
[src]
Checks whether a frame is ready to be received.
pub async fn receive<'_>(&'_ mut self) -> Result<Frame, WebSocketError>
[src]
Receives a Frame
over the WebSocket connection.
If the received frame is a Ping frame, a Pong frame will be sent. If the received frame is a Close frame, an echoed Close frame will be sent and the WebSocket will close.
This method may block until receiving is ready. To check whether
data is available to be received, use ready_to_receive()
.
pub async fn send_text<'_>(
&'_ mut self,
payload: String,
continuation: bool,
fin: bool
) -> Result<(), WebSocketError>
[src]
&'_ mut self,
payload: String,
continuation: bool,
fin: bool
) -> Result<(), WebSocketError>
Sends a Text frame over the WebSocket connection, constructed from passed arguments.
pub async fn send_binary<'_>(
&'_ mut self,
payload: Vec<u8>,
continuation: bool,
fin: bool
) -> Result<(), WebSocketError>
[src]
&'_ mut self,
payload: Vec<u8>,
continuation: bool,
fin: bool
) -> Result<(), WebSocketError>
Sends a Binary frame over the WebSocket connection, constructed from passed arguments.
pub async fn close<'_>(
&'_ mut self,
payload: Option<(u16, String)>
) -> Result<Frame, WebSocketError>
[src]
&'_ mut self,
payload: Option<(u16, String)>
) -> Result<Frame, WebSocketError>
Sends a Close frame over the WebSocket connection, constructed from passed arguments, and closes the WebSocket connection. This method will attempt to wait for an echoed Close frame, which is returned.
pub async fn send_ping<'_>(
&'_ mut self,
payload: Option<Vec<u8>>
) -> Result<(), WebSocketError>
[src]
&'_ mut self,
payload: Option<Vec<u8>>
) -> Result<(), WebSocketError>
Sends a Ping frame over the WebSocket connection, constructed from passed arguments.
pub async fn send_pong<'_>(
&'_ mut self,
payload: Option<Vec<u8>>
) -> Result<(), WebSocketError>
[src]
&'_ mut self,
payload: Option<Vec<u8>>
) -> Result<(), WebSocketError>
Sends a Pong frame over the WebSocket connection, constructed from passed arguments.
pub async fn shutdown<'_>(&'_ mut self) -> Result<(), WebSocketError>
[src]
Shuts down the WebSocket connection without sending a Close frame.
It is recommended to use the close()
method instead.
pub fn accepted_subprotocol(&self) -> &Option<String>
[src]
Returns the subprotocol that was accepted by the server during the handshake, if any.
pub fn handshake_response_headers(&self) -> &Option<Vec<(String, String)>>
[src]
Returns the headers that were returned by the server during the handshake.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for WebSocket
impl Send for WebSocket
impl Sync for WebSocket
impl Unpin for WebSocket
impl !UnwindSafe for WebSocket
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,