pub struct WebSocketClient { /* private fields */ }Expand description
WebSocket client with automatic reconnection (thin wrapper over ConnectionSupervisor)
Implementations§
Source§impl WebSocketClient
impl WebSocketClient
Sourcepub fn builder(uri: impl Into<String>) -> WebSocketClientBuilder
pub fn builder(uri: impl Into<String>) -> WebSocketClientBuilder
Create a new WebSocket client builder
Sourcepub fn subscribe(&self) -> Receiver<SharedMessage>
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
Sourcepub fn subscribe_events(&self) -> Receiver<ConnectionEvent>
pub fn subscribe_events(&self) -> Receiver<ConnectionEvent>
Subscribe to connection events
Sourcepub async fn send(&self, message: Message) -> Result<(), SendError>
pub async fn send(&self, message: Message) -> Result<(), SendError>
Send a message (convenience method)
Sourcepub async fn send_async(&self, message: Message) -> Result<(), SendError>
pub async fn send_async(&self, message: Message) -> Result<(), SendError>
Send a message (async, blocking on capacity)
Sourcepub async fn send_timeout(
&self,
message: Message,
timeout: Duration,
) -> Result<(), SendError>
pub async fn send_timeout( &self, message: Message, timeout: Duration, ) -> Result<(), SendError>
Send a message with timeout (async)
Sourcepub async fn state(&self) -> ConnectionSnapshot
pub async fn state(&self) -> ConnectionSnapshot
Get current connection state snapshot
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Check if currently connected
Sourcepub async fn register_extension<E: Extension + 'static>(
&self,
extension: E,
) -> Result<(), ClientError>
pub async fn register_extension<E: Extension + 'static>( &self, extension: E, ) -> Result<(), ClientError>
Register an extension
Sourcepub async fn run(&self) -> Result<(), ClientError>
pub async fn run(&self) -> Result<(), ClientError>
Run the client (blocking)
Sourcepub async fn shutdown_graceful(
&self,
timeout: Duration,
) -> Result<(), ClientError>
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.