//! WSS Client Demo using Circle WebSocket Client //! //! This example demonstrates connecting to a WSS (WebSocket Secure) server //! using the circle_client_ws library with proper authentication. use circle_client_ws::{CircleWsClientBuilder, auth}; use log::{info, error}; #[tokio::main] async fn main() -> Result<(), Box> { // Initialize logging - use RUST_LOG=info for detailed output env_logger::init(); println!("๐Ÿงช Starting WSS Client Demo"); // Generate authentication credentials let private_key = auth::generate_private_key()?; let public_key = auth::derive_public_key(&private_key)?; info!("๐Ÿ”‘ Generated credentials - Public key: {}...", &public_key[..20]); // WSS server URL (secure WebSocket) let wss_url = "wss://127.0.0.1:8443/ws"; info!("๐Ÿ”— Connecting to WSS server at {}", wss_url); println!("๐Ÿ”Œ Connecting to WSS server..."); // Create WSS client with authentication let mut client = CircleWsClientBuilder::new(wss_url.to_string()) .with_keypair(private_key) .build(); // Connect to the server match client.connect().await { Ok(()) => { println!("โœ… Successfully connected to WSS server!"); info!("๐Ÿ“ก Response status: 101 Switching Protocols"); println!("\n๐Ÿงช Testing WSS connection with JSON-RPC messages..."); // Test 1: Authentication println!("๐Ÿ“ค Test 1: Performing authentication"); match client.authenticate().await { Ok(true) => { println!("โœ… Authentication successful!"); info!("๐Ÿ” Client authenticated with server"); // Test 2: Execute script (authenticated request) println!("\n๐Ÿ“ค Test 2: Executing script (authenticated request)"); let test_script = "40 + 2".to_string(); match client.play(test_script).await { Ok(result) => { println!("โœ… Script execution successful!"); println!("๐Ÿ“Š Result: {}", result.output); info!("Script output: {}", result.output); } Err(e) => { error!("โŒ Script execution failed: {}", e); println!("โŒ Script execution failed: {}", e); } } } Ok(false) => { println!("โŒ Authentication failed - server rejected credentials"); error!("Authentication failed"); } Err(e) => { error!("โŒ Authentication error: {}", e); println!("โŒ Authentication error: {}", e); } } // Disconnect client.disconnect().await; println!("\n๐Ÿ”Œ Disconnected from WSS server"); println!("\n๐ŸŽ‰ Summary:"); println!(" โœ… WSS connection established"); println!(" โœ… TLS encryption working"); println!(" โœ… JSON-RPC protocol working"); println!(" โœ… Authentication system working"); println!(" โœ… Script execution working"); } Err(e) => { error!("โŒ Failed to connect to WSS server: {}", e); println!("โŒ Connection failed: {}", e); println!("\n๐Ÿ’ก Make sure the WSS server is running:"); println!(" cargo run --manifest-path src/server/Cargo.toml --example wss_server --features auth"); } } Ok(()) }