Skip to main content

WebSocket

Struct WebSocket 

Source
pub struct WebSocket {
    pub url: String,
    /* private fields */
}
Expand description

W3C WebSocket client interface supporting ws:// and TLS wss://.

Fields§

§url: String

Implementations§

Source§

impl WebSocket

Source

pub fn new(url: &str) -> WebSocket

Creates an in-memory/mock WebSocket for offline testing.

Source

pub fn connect(url: &str) -> Result<WebSocket, WsError>

Establishes a real RFC 6455 WebSocket connection over plain TCP (ws://) or TLS (wss://).

Source

pub fn set_open(&self)

Mark the connection as Open.

Source

pub fn ready_state(&self) -> WsState

Current connection state.

Source

pub fn is_open(&self) -> bool

Whether the WebSocket is open.

Source

pub fn send_text(&self, text: &str) -> Result<(), WsError>

Sends a text message over the WebSocket. If length exceeds DEFAULT_MAX_FRAME_SIZE, it is fragmented.

Source

pub fn send_binary(&self, data: &[u8]) -> Result<(), WsError>

Sends binary data over the WebSocket. If length exceeds DEFAULT_MAX_FRAME_SIZE, it is fragmented.

Source

pub fn send_text_fragmented( &self, text: &str, chunk_size: usize, ) -> Result<(), WsError>

Sends a text message split into multiple frames with given chunk size.

Source

pub fn send_binary_fragmented( &self, data: &[u8], chunk_size: usize, ) -> Result<(), WsError>

Sends binary data split into multiple frames with given chunk size.

Source

pub fn send_payload_fragmented( &self, opcode: WsOpcode, payload: &[u8], chunk_size: usize, ) -> Result<(), WsError>

Helper to send payload in fragmented RFC 6455 frames.

Source

pub fn send_ping(&self, payload: &[u8]) -> Result<(), WsError>

Sends a Ping frame.

Source

pub fn close(&self, code: u16, reason: &str) -> Result<(), WsError>

Closes the WebSocket connection with status code and reason.

Source

pub fn ingest_incoming_bytes(&self, buf: &[u8]) -> Result<usize, WsError>

Ingests received raw bytes and decodes messages into the internal buffer.

Source

pub fn poll_message(&self) -> Option<WsMessage>

Pops the next received message from the queue, if any.

Source

pub fn recv_message_timeout(&self, timeout: Duration) -> Option<WsMessage>

Waits up to timeout for a message to arrive.

Source

pub fn drain_outgoing_frames(&self) -> Vec<Vec<u8>>

Drains all currently queued outgoing encoded frames.

Trait Implementations§

Source§

impl Clone for WebSocket

Source§

fn clone(&self) -> WebSocket

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Drop for WebSocket

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoInitialState<T> for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,