43 lines
1.5 KiB
Rust
43 lines
1.5 KiB
Rust
use circle_client_ws::CircleWsClient;
|
|
use std::env;
|
|
use tracing_subscriber::EnvFilter;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
tracing_subscriber::fmt()
|
|
.with_env_filter(EnvFilter::from_default_env().add_directive("connect_and_play=info".parse().unwrap()).add_directive("circle_client_ws=info".parse().unwrap()))
|
|
.init();
|
|
|
|
let args: Vec<String> = env::args().collect();
|
|
let ws_url = args.get(1).cloned().unwrap_or_else(|| {
|
|
let default_url = "ws://127.0.0.1:8081/ws".to_string();
|
|
println!("No WebSocket URL provided. Defaulting to: {}", default_url);
|
|
default_url
|
|
});
|
|
|
|
println!("Attempting to connect to {}...", ws_url);
|
|
let mut client = CircleWsClient::new(ws_url.clone());
|
|
|
|
if let Err(e) = client.connect().await {
|
|
eprintln!("Failed to connect to {}: {}", ws_url, e);
|
|
return Err(e.into());
|
|
}
|
|
println!("Connected to {}!", ws_url);
|
|
|
|
let script = "let a = 10; let b = 32; let message = \"Hello from example script!\"; message + \" Result: \" + (a + b)";
|
|
println!("\nSending script:\n```rhai\n{}\n```", script);
|
|
|
|
match client.play(script.to_string()).await {
|
|
Ok(play_result) => {
|
|
println!("\nServer response:\nOutput: {}", play_result.output);
|
|
}
|
|
Err(e) => {
|
|
eprintln!("\nError executing script: {}", e);
|
|
}
|
|
}
|
|
|
|
client.disconnect().await;
|
|
println!("\nDisconnected from {}.", ws_url);
|
|
|
|
Ok(())
|
|
} |