pub struct Client<S: AppState> {
pub signal: Signals,
pub state: Arc<S>,
pub module_handles: Arc<RwLock<HashMap<TypeId, Box<dyn Any + Send + Sync>>>>,
pub to_ws_sender: AsyncSender<Message>,
/* private fields */
}Fields§
§signal: Signals§state: Arc<S>The shared application state, which can be used by modules and handlers.
module_handles: Arc<RwLock<HashMap<TypeId, Box<dyn Any + Send + Sync>>>>§to_ws_sender: AsyncSender<Message>Implementations§
Source§impl<S: AppState> Client<S>
impl<S: AppState> Client<S>
pub fn new( signal: Signals, runner_command_tx: AsyncSender<RunnerCommand>, state: Arc<S>, sender: AsyncSender<Message>, ) -> Self
Sourcepub async fn wait_connected(&self)
pub async fn wait_connected(&self)
Waits until the client is connected to the WebSocket server. This method will block until the connection is established. It is useful for ensuring that the client is ready to send and receive messages.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Checks if the client is connected to the WebSocket server.
Sourcepub async fn get_handle<M: ApiModule<S>>(&self) -> Option<M::Handle>
pub async fn get_handle<M: ApiModule<S>>(&self) -> Option<M::Handle>
Retrieves a clonable, typed handle to an already-registered module.
Examples found in repository?
317 pub async fn echo(&self, msg: String) -> CoreResult<String> {
318 match self.client.get_handle::<EchoModule>().await {
319 Some(echo_handle) => echo_handle.echo(msg).await,
320 None => Err(CoreError::ModuleNotFound("EchoModule".to_string())),
321 }
322 }
323
324 pub async fn stream(&self) -> CoreResult<impl Stream<Item = CoreResult<String>>> {
325 let stream_handle = self.client.get_handle::<StreamModule>().await.unwrap();
326 println!("Starting stream...");
327 stream_handle.stream().await
328 }
329
330 pub async fn start(&self) -> CoreResult<()> {
331 match self.client.get_handle::<PeriodicSenderModule>().await {
332 Some(handle) => {
333 handle.start().await;
334 Ok(())
335 }
336 None => Err(CoreError::ModuleNotFound(
337 stringify!(PeriodicSenderModule).to_string(),
338 )),
339 }
340 }More examples
Sourcepub async fn disconnect(&self) -> CoreResult<()>
pub async fn disconnect(&self) -> CoreResult<()>
Commands the runner to disconnect, clear state, and perform a “hard” reconnect.
Sourcepub async fn reconnect(&self) -> CoreResult<()>
pub async fn reconnect(&self) -> CoreResult<()>
Commands the runner to disconnect, and perform a “soft” reconnect.
Sourcepub async fn shutdown(self) -> CoreResult<()>
pub async fn shutdown(self) -> CoreResult<()>
Commands the runner to shutdown, this action is final as the runner and client will stop working and will be dropped.
Examples found in repository?
219async fn main() -> CoreResult<()> {
220 // Initialize tracing
221 tracing_subscriber::fmt::init();
222
223 // Create statistics middleware
224 let stats_middleware = Arc::new(StatisticsMiddleware::new());
225
226 // Build the client with middleware
227 let (client, _) = ClientBuilder::new(MockConnector, ExampleState)
228 .with_middleware(Box::new(LoggingMiddleware))
229 .with_middleware(Box::new(StatisticsMiddleware::new()))
230 .with_module::<ExampleModule>()
231 .build()
232 .await?;
233
234 info!("Client built with middleware layers");
235 tokio::time::sleep(Duration::from_secs(10)).await;
236 client.shutdown().await?;
237 // In a real application, you would:
238 // 1. Start the runner in a background task
239 // 2. Use the client to send messages
240 // 3. Check statistics periodically
241
242 // For demonstration, we'll just show the statistics
243 let stats = stats_middleware.get_stats();
244 info!("Current statistics: {:?}", stats);
245
246 Ok(())
247}Sourcepub async fn shutdown_ref(&self) -> CoreResult<()>
pub async fn shutdown_ref(&self) -> CoreResult<()>
Commands the runner to shutdown without consuming the client.
Sourcepub async fn send_message(&self, message: Message) -> CoreResult<()>
pub async fn send_message(&self, message: Message) -> CoreResult<()>
Send a message to the WebSocket
Sourcepub async fn send_text(&self, text: String) -> CoreResult<()>
pub async fn send_text(&self, text: String) -> CoreResult<()>
Send a text message to the WebSocket
Sourcepub async fn send_binary(&self, data: Vec<u8>) -> CoreResult<()>
pub async fn send_binary(&self, data: Vec<u8>) -> CoreResult<()>
Send a binary message to the WebSocket