use ibapi::Client;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let address = "127.0.0.1:4002"; let client_id = 100;
println!("Connecting to {} with client ID {}...", address, client_id);
let client = Client::connect(address, client_id).await?;
println!("Successfully connected to TWS/Gateway");
println!("Server version: {}", client.server_version());
if client.is_connected() {
println!("✓ Client is connected");
} else {
println!("✗ Client is not connected");
}
println!("\nMonitoring connection status (press Ctrl+C to exit)...");
let mut connected_previously = true;
loop {
let is_connected = client.is_connected();
if is_connected != connected_previously {
if is_connected {
println!("✓ Connection restored");
} else {
println!("✗ Connection lost");
}
connected_previously = is_connected;
}
if is_connected {
match client.server_time().await {
Ok(time) => {
println!("Server time: {}", time);
}
Err(e) => {
println!("Error getting server time: {}", e);
}
}
} else {
println!("Waiting for connection to be restored...");
}
sleep(Duration::from_secs(5)).await;
}
}