WebSocketClient

Struct WebSocketClient 

Source
pub struct WebSocketClient { /* private fields */ }
Expand description

WebSocket client with automatic reconnection (thin wrapper over ConnectionSupervisor)

Implementations§

Source§

impl WebSocketClient

Source

pub fn builder(uri: impl Into<String>) -> WebSocketClientBuilder

Create a new WebSocket client builder

Source

pub fn new(uri: impl Into<String>) -> Self

Create a client with default configuration

Source

pub fn subscribe(&self) -> Receiver<SharedMessage>

Subscribe to incoming messages

Returns a receiver for shared messages wrapped in Arc<Message> for zero-copy broadcasting. To work with the message:

  • Read-only access: msg.as_ref() or dereference &*msg
  • Need owned copy: Arc::try_unwrap(msg).unwrap_or_else(|arc| (*arc).clone())
  • Clone specific data: msg.clone() clones the Arc (cheap), (*msg).clone() clones the Message
Source

pub fn uri(&self) -> &str

Get target URI

Source

pub fn subscribe_events(&self) -> Receiver<ConnectionEvent>

Subscribe to connection events

Source

pub async fn sender(&self) -> Option<Sender>

Get a sender handle for sending messages

Source

pub async fn send(&self, message: Message) -> Result<(), SendError>

Send a message (convenience method)

Source

pub async fn send_async(&self, message: Message) -> Result<(), SendError>

Send a message (async, blocking on capacity)

Source

pub async fn send_timeout( &self, message: Message, timeout: Duration, ) -> Result<(), SendError>

Send a message with timeout (async)

Source

pub async fn state(&self) -> ConnectionSnapshot

Get current connection state snapshot

Source

pub fn is_connected(&self) -> bool

Check if currently connected

Source

pub async fn register_extension<E: Extension + 'static>( &self, extension: E, ) -> Result<(), ClientError>

Register an extension

Source

pub async fn run(&self) -> Result<(), ClientError>

Run the client (blocking)

Source

pub fn shutdown(&self)

Shutdown the client

Source

pub async fn shutdown_graceful( &self, timeout: Duration, ) -> Result<(), ClientError>

Shutdown the client gracefully, waiting for the run loop to exit or timing out.

This method triggers shutdown() and then waits for the run loop to finish. If the client is not running, it returns immediately.

Note: This only waits for the run loop to exit (extensions receive disconnect/shutdown hooks). It does not guarantee that pending user messages make it to the peer; callers should coordinate with their own protocols if that guarantee is required.

§Errors

Returns ClientError::ShutdownTimeout if the run loop does not exit within the specified timeout duration.

Trait Implementations§

Source§

impl WebSocketClientExt for WebSocketClient

Source§

fn spawn(self: Arc<Self>) -> JoinHandle<Result<(), ClientError>>

Spawn the client as a background task

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more