socktop/socktop_connector/examples/wasm_example.rs

39 lines
1.3 KiB
Rust
Raw Permalink Normal View History

//! Example of using socktop_connector in a WASM environment.
//!
//! This example demonstrates how to use the connector without TLS dependencies
//! for WebAssembly builds.
2025-09-08 19:29:03 +00:00
use socktop_connector::{AgentRequest, ConnectorConfig, connect_to_socktop_agent};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("WASM-compatible socktop connector example");
2025-09-08 19:29:03 +00:00
// For WASM builds, use ws:// (not wss://) to avoid TLS dependencies
let url = "ws://localhost:3000/ws";
2025-09-08 19:29:03 +00:00
// Method 1: Simple connection (recommended for most use cases)
let mut connector = connect_to_socktop_agent(url).await?;
2025-09-08 19:29:03 +00:00
// Method 2: With custom WebSocket configuration
let config = ConnectorConfig::new(url)
.with_protocols(vec!["socktop".to_string()])
.with_version("13".to_string());
2025-09-08 19:29:03 +00:00
let mut connector_custom = socktop_connector::SocktopConnector::new(config);
connector_custom.connect().await?;
2025-09-08 19:29:03 +00:00
// Make a request to get metrics
match connector.request(AgentRequest::Metrics).await {
Ok(response) => {
println!("Successfully received response: {response:?}");
}
Err(e) => {
println!("Request failed: {e}");
}
}
2025-09-08 19:29:03 +00:00
println!("WASM example completed successfully!");
Ok(())
}