pub struct WebSocketClient<T>where
T: DeserializeOwned,{ /* private fields */ }Expand description
High-level WebSocket client with automatic reconnection, ping/pong handling, and JSON support
Implementations§
Source§impl<T> WebSocketClient<T>where
T: DeserializeOwned,
impl<T> WebSocketClient<T>where
T: DeserializeOwned,
Sourcepub fn new(config: WsConnectionConfig) -> Self
pub fn new(config: WsConnectionConfig) -> Self
Create a new WebSocket client with the given configuration
Sourcepub async fn connect(&self) -> Result<(WsWriter, WsReader)>
pub async fn connect(&self) -> Result<(WsWriter, WsReader)>
Connect to the WebSocket server without sending any subscription
Returns a writer and reader for manual message handling.
Sourcepub async fn connect_stream(&self) -> Result<MessageStream<T>>
pub async fn connect_stream(&self) -> Result<MessageStream<T>>
Connect to the WebSocket server and return a MessageStream
The MessageStream automatically handles PING/PONG and yields parsed messages.
Sourcepub async fn connect_and_subscribe<S: Serialize>(
&self,
subscription: Option<&S>,
) -> Result<(WsWriter, WsReader)>
pub async fn connect_and_subscribe<S: Serialize>( &self, subscription: Option<&S>, ) -> Result<(WsWriter, WsReader)>
Connect and optionally send an initial subscription message
Deprecated: Use connect() + send_subscription() or connect_stream() instead.
Sourcepub async fn listen<S, F>(
&self,
subscription: Option<S>,
handler: F,
) -> Result<()>
pub async fn listen<S, F>( &self, subscription: Option<S>, handler: F, ) -> Result<()>
Start listening to messages with automatic reconnection and ping/pong handling
Uses exponential backoff for reconnection attempts (same config as initial connection).
§Arguments
subscription- Optional subscription message to send on each connectionhandler- Callback function that processes each received message
§Example
let client = WebSocketClient::<MyMessage>::new(config);
client.listen(Some(&subscription), |msg| {
println!("Received: {:?}", msg);
Ok(())
}).await?;Trait Implementations§
Source§impl<T> Clone for WebSocketClient<T>where
T: DeserializeOwned + Clone,
impl<T> Clone for WebSocketClient<T>where
T: DeserializeOwned + Clone,
Source§fn clone(&self) -> WebSocketClient<T>
fn clone(&self) -> WebSocketClient<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl<T> Freeze for WebSocketClient<T>
impl<T> RefUnwindSafe for WebSocketClient<T>where
T: RefUnwindSafe,
impl<T> Send for WebSocketClient<T>where
T: Send,
impl<T> Sync for WebSocketClient<T>where
T: Sync,
impl<T> Unpin for WebSocketClient<T>where
T: Unpin,
impl<T> UnwindSafe for WebSocketClient<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more