pub struct ClientBuilder<S: AppState> { /* private fields */ }Implementations§
Source§impl<S: AppState> ClientBuilder<S>
impl<S: AppState> ClientBuilder<S>
Sourcepub fn new(connector: impl Connector<S> + 'static, state: S) -> Self
pub fn new(connector: impl Connector<S> + 'static, state: S) -> Self
Creates a new builder with the essential components.
Examples found in repository?
examples/echo_client.rs (line 292)
288 pub async fn new(url: String) -> CoreResult<Self> {
289 // Use a simple connector (implement your own if needed)
290 let connector = DummyConnector::new(url);
291
292 let mut builder = ClientBuilder::new(connector, ());
293 builder =
294 builder.with_lightweight_handler(|msg, state, _| Box::pin(print_handler(msg, state)));
295 let (client, mut runner) = builder
296 .with_module::<EchoModule>()
297 .with_module::<StreamModule>()
298 .with_module::<PeriodicSenderModule>()
299 .build()
300 .await?;
301
302 // let echo_handle = client.get_handle::<EchoModule>().await.unwrap();
303 // let stream_handle = client.get_handle::<StreamModule>().await.unwrap();
304
305 // Start runner in background
306 let _runner = tokio::spawn(async move { runner.run().await });
307
308 Ok(Self { client, _runner })
309 }More examples
examples/middleware_example.rs (line 226)
218async fn main() -> CoreResult<()> {
219 // Initialize tracing
220 tracing_subscriber::fmt::init();
221
222 // Create statistics middleware
223 let stats_middleware = Arc::new(StatisticsMiddleware::new());
224
225 // Build the client with middleware
226 let (client, _) = ClientBuilder::new(MockConnector, ExampleState)
227 .with_middleware(Box::new(LoggingMiddleware))
228 .with_middleware(Box::new(StatisticsMiddleware::new()))
229 .with_module::<ExampleModule>()
230 .build()
231 .await?;
232
233 info!("Client built with middleware layers");
234 tokio::time::sleep(Duration::from_secs(10)).await;
235 client.shutdown().await?;
236 // In a real application, you would:
237 // 1. Start the runner in a background task
238 // 2. Use the client to send messages
239 // 3. Check statistics periodically
240
241 // For demonstration, we'll just show the statistics
242 let stats = stats_middleware.get_stats();
243 info!("Current statistics: {:?}", stats);
244
245 Ok(())
246}examples/testing_echo_client.rs (line 124)
121 pub async fn new(url: String) -> CoreResult<Self> {
122 let connector = DummyConnector::new(url);
123
124 let builder = ClientBuilder::new(connector, ()).with_module::<EchoModule>();
125
126 // // Create testing wrapper with custom configuration
127 // let testing_config = TestingConfig {
128 // stats_interval: Duration::from_secs(10), // Log stats every 10 seconds
129 // log_stats: true,
130 // track_events: true,
131 // max_reconnect_attempts: Some(3),
132 // reconnect_delay: Duration::from_secs(5),
133 // connection_timeout: Duration::from_secs(30),
134 // auto_reconnect: true,
135 // };
136
137 let testing_wrapper = TestingWrapperBuilder::new()
138 .with_stats_interval(Duration::from_secs(10))
139 .with_log_stats(true)
140 .with_track_events(true)
141 .with_max_reconnect_attempts(Some(3))
142 .with_reconnect_delay(Duration::from_secs(5))
143 .with_connection_timeout(Duration::from_secs(30))
144 .with_auto_reconnect(true)
145 .build_with_middleware(builder)
146 .await?;
147
148 Ok(Self { testing_wrapper })
149 }Sourcepub fn on_connect(
self,
callback: impl Fn(Arc<S>, &AsyncSender<Message>) -> BoxFuture<'static, CoreResult<()>> + Send + Sync + 'static,
) -> Self
pub fn on_connect( self, callback: impl Fn(Arc<S>, &AsyncSender<Message>) -> BoxFuture<'static, CoreResult<()>> + Send + Sync + 'static, ) -> Self
Sets the callback for the initial connection.
Sourcepub fn on_reconnect(
self,
callback: Box<dyn ReconnectCallback<S> + Send + Sync + 'static>,
) -> Self
pub fn on_reconnect( self, callback: Box<dyn ReconnectCallback<S> + Send + Sync + 'static>, ) -> Self
Sets the callback for subsequent reconnections.
Sourcepub fn with_lightweight_handler(
self,
handler: impl Fn(Arc<Message>, Arc<S>, &AsyncSender<Message>) -> BoxFuture<'static, CoreResult<()>> + Send + Sync + 'static,
) -> Self
pub fn with_lightweight_handler( self, handler: impl Fn(Arc<Message>, Arc<S>, &AsyncSender<Message>) -> BoxFuture<'static, CoreResult<()>> + Send + Sync + 'static, ) -> Self
Adds a lightweight handler that receives all messages.
Examples found in repository?
examples/echo_client.rs (line 294)
288 pub async fn new(url: String) -> CoreResult<Self> {
289 // Use a simple connector (implement your own if needed)
290 let connector = DummyConnector::new(url);
291
292 let mut builder = ClientBuilder::new(connector, ());
293 builder =
294 builder.with_lightweight_handler(|msg, state, _| Box::pin(print_handler(msg, state)));
295 let (client, mut runner) = builder
296 .with_module::<EchoModule>()
297 .with_module::<StreamModule>()
298 .with_module::<PeriodicSenderModule>()
299 .build()
300 .await?;
301
302 // let echo_handle = client.get_handle::<EchoModule>().await.unwrap();
303 // let stream_handle = client.get_handle::<StreamModule>().await.unwrap();
304
305 // Start runner in background
306 let _runner = tokio::spawn(async move { runner.run().await });
307
308 Ok(Self { client, _runner })
309 }Sourcepub fn with_lightweight_module<M: LightweightModule<S>>(self) -> Self
pub fn with_lightweight_module<M: LightweightModule<S>>(self) -> Self
Registers a lightweight module
Sourcepub fn with_module<M: ApiModule<S>>(self) -> Self
pub fn with_module<M: ApiModule<S>>(self) -> Self
Registers a full API module with the client.
Examples found in repository?
examples/echo_client.rs (line 296)
288 pub async fn new(url: String) -> CoreResult<Self> {
289 // Use a simple connector (implement your own if needed)
290 let connector = DummyConnector::new(url);
291
292 let mut builder = ClientBuilder::new(connector, ());
293 builder =
294 builder.with_lightweight_handler(|msg, state, _| Box::pin(print_handler(msg, state)));
295 let (client, mut runner) = builder
296 .with_module::<EchoModule>()
297 .with_module::<StreamModule>()
298 .with_module::<PeriodicSenderModule>()
299 .build()
300 .await?;
301
302 // let echo_handle = client.get_handle::<EchoModule>().await.unwrap();
303 // let stream_handle = client.get_handle::<StreamModule>().await.unwrap();
304
305 // Start runner in background
306 let _runner = tokio::spawn(async move { runner.run().await });
307
308 Ok(Self { client, _runner })
309 }More examples
examples/middleware_example.rs (line 229)
218async fn main() -> CoreResult<()> {
219 // Initialize tracing
220 tracing_subscriber::fmt::init();
221
222 // Create statistics middleware
223 let stats_middleware = Arc::new(StatisticsMiddleware::new());
224
225 // Build the client with middleware
226 let (client, _) = ClientBuilder::new(MockConnector, ExampleState)
227 .with_middleware(Box::new(LoggingMiddleware))
228 .with_middleware(Box::new(StatisticsMiddleware::new()))
229 .with_module::<ExampleModule>()
230 .build()
231 .await?;
232
233 info!("Client built with middleware layers");
234 tokio::time::sleep(Duration::from_secs(10)).await;
235 client.shutdown().await?;
236 // In a real application, you would:
237 // 1. Start the runner in a background task
238 // 2. Use the client to send messages
239 // 3. Check statistics periodically
240
241 // For demonstration, we'll just show the statistics
242 let stats = stats_middleware.get_stats();
243 info!("Current statistics: {:?}", stats);
244
245 Ok(())
246}examples/testing_echo_client.rs (line 124)
121 pub async fn new(url: String) -> CoreResult<Self> {
122 let connector = DummyConnector::new(url);
123
124 let builder = ClientBuilder::new(connector, ()).with_module::<EchoModule>();
125
126 // // Create testing wrapper with custom configuration
127 // let testing_config = TestingConfig {
128 // stats_interval: Duration::from_secs(10), // Log stats every 10 seconds
129 // log_stats: true,
130 // track_events: true,
131 // max_reconnect_attempts: Some(3),
132 // reconnect_delay: Duration::from_secs(5),
133 // connection_timeout: Duration::from_secs(30),
134 // auto_reconnect: true,
135 // };
136
137 let testing_wrapper = TestingWrapperBuilder::new()
138 .with_stats_interval(Duration::from_secs(10))
139 .with_log_stats(true)
140 .with_track_events(true)
141 .with_max_reconnect_attempts(Some(3))
142 .with_reconnect_delay(Duration::from_secs(5))
143 .with_connection_timeout(Duration::from_secs(30))
144 .with_auto_reconnect(true)
145 .build_with_middleware(builder)
146 .await?;
147
148 Ok(Self { testing_wrapper })
149 }Sourcepub fn with_middleware(
self,
middleware: Box<dyn WebSocketMiddleware<S>>,
) -> Self
pub fn with_middleware( self, middleware: Box<dyn WebSocketMiddleware<S>>, ) -> Self
Adds a middleware layer to the client.
Middleware will be executed in the order they are added. They will be called for all WebSocket messages sent and received.
§Example
let builder = ClientBuilder::new(MyConnector, MyState)
.with_middleware(Box::new(MyMiddleware));Examples found in repository?
examples/middleware_example.rs (line 227)
218async fn main() -> CoreResult<()> {
219 // Initialize tracing
220 tracing_subscriber::fmt::init();
221
222 // Create statistics middleware
223 let stats_middleware = Arc::new(StatisticsMiddleware::new());
224
225 // Build the client with middleware
226 let (client, _) = ClientBuilder::new(MockConnector, ExampleState)
227 .with_middleware(Box::new(LoggingMiddleware))
228 .with_middleware(Box::new(StatisticsMiddleware::new()))
229 .with_module::<ExampleModule>()
230 .build()
231 .await?;
232
233 info!("Client built with middleware layers");
234 tokio::time::sleep(Duration::from_secs(10)).await;
235 client.shutdown().await?;
236 // In a real application, you would:
237 // 1. Start the runner in a background task
238 // 2. Use the client to send messages
239 // 3. Check statistics periodically
240
241 // For demonstration, we'll just show the statistics
242 let stats = stats_middleware.get_stats();
243 info!("Current statistics: {:?}", stats);
244
245 Ok(())
246}Sourcepub fn with_middleware_layers(
self,
middleware: Vec<Box<dyn WebSocketMiddleware<S>>>,
) -> Self
pub fn with_middleware_layers( self, middleware: Vec<Box<dyn WebSocketMiddleware<S>>>, ) -> Self
Adds multiple middleware layers at once.
This is a convenience method for adding multiple middleware layers.
§Example
let builder = ClientBuilder::new(MyConnector, MyState)
.with_middleware_layers(vec![
Box::new(MyMiddleware),
Box::new(MyMiddleware),
]);Sourcepub fn with_middleware_stack(self, stack: MiddlewareStack<S>) -> Self
pub fn with_middleware_stack(self, stack: MiddlewareStack<S>) -> Self
Applies a middleware stack to the client.
This replaces any existing middleware with the provided stack.
§Example
let mut stack = MiddlewareStack::new();
stack.add_layer(Box::new(MyMiddleware));
let builder = ClientBuilder::new(MyConnector, MyState)
.with_middleware_stack(stack);Sourcepub async fn build(self) -> CoreResult<(Client<S>, ClientRunner<S>)>
pub async fn build(self) -> CoreResult<(Client<S>, ClientRunner<S>)>
Assembles and returns the final Client handle and its ClientRunner.
Examples found in repository?
examples/echo_client.rs (line 299)
288 pub async fn new(url: String) -> CoreResult<Self> {
289 // Use a simple connector (implement your own if needed)
290 let connector = DummyConnector::new(url);
291
292 let mut builder = ClientBuilder::new(connector, ());
293 builder =
294 builder.with_lightweight_handler(|msg, state, _| Box::pin(print_handler(msg, state)));
295 let (client, mut runner) = builder
296 .with_module::<EchoModule>()
297 .with_module::<StreamModule>()
298 .with_module::<PeriodicSenderModule>()
299 .build()
300 .await?;
301
302 // let echo_handle = client.get_handle::<EchoModule>().await.unwrap();
303 // let stream_handle = client.get_handle::<StreamModule>().await.unwrap();
304
305 // Start runner in background
306 let _runner = tokio::spawn(async move { runner.run().await });
307
308 Ok(Self { client, _runner })
309 }More examples
examples/middleware_example.rs (line 230)
218async fn main() -> CoreResult<()> {
219 // Initialize tracing
220 tracing_subscriber::fmt::init();
221
222 // Create statistics middleware
223 let stats_middleware = Arc::new(StatisticsMiddleware::new());
224
225 // Build the client with middleware
226 let (client, _) = ClientBuilder::new(MockConnector, ExampleState)
227 .with_middleware(Box::new(LoggingMiddleware))
228 .with_middleware(Box::new(StatisticsMiddleware::new()))
229 .with_module::<ExampleModule>()
230 .build()
231 .await?;
232
233 info!("Client built with middleware layers");
234 tokio::time::sleep(Duration::from_secs(10)).await;
235 client.shutdown().await?;
236 // In a real application, you would:
237 // 1. Start the runner in a background task
238 // 2. Use the client to send messages
239 // 3. Check statistics periodically
240
241 // For demonstration, we'll just show the statistics
242 let stats = stats_middleware.get_stats();
243 info!("Current statistics: {:?}", stats);
244
245 Ok(())
246}Auto Trait Implementations§
impl<S> Freeze for ClientBuilder<S>
impl<S> !RefUnwindSafe for ClientBuilder<S>
impl<S> Send for ClientBuilder<S>
impl<S> Sync for ClientBuilder<S>
impl<S> Unpin for ClientBuilder<S>
impl<S> !UnwindSafe for ClientBuilder<S>
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