()
| 77 | |
| 78 | #[tokio::main] |
| 79 | async fn main() -> anyhow::Result<()> { |
| 80 | println!("==========================================="); |
| 81 | println!(" Buttplug Rust Application Example"); |
| 82 | println!("===========================================\n"); |
| 83 | |
| 84 | // Step 1: Create a client |
| 85 | // The client name identifies your application to the server. |
| 86 | let client = ButtplugClient::new("My Buttplug Application"); |
| 87 | |
| 88 | // Step 2: Set up event handlers |
| 89 | // Get the event stream BEFORE connecting to avoid missing events. |
| 90 | let mut events = client.event_stream(); |
| 91 | |
| 92 | tokio::spawn(async move { |
| 93 | while let Some(event) = events.next().await { |
| 94 | match event { |
| 95 | ButtplugClientEvent::DeviceAdded(device) => { |
| 96 | println!("[+] Device connected: {}", device.name()); |
| 97 | } |
| 98 | ButtplugClientEvent::DeviceRemoved(info) => { |
| 99 | println!("[-] Device disconnected: {}", info.name()); |
| 100 | } |
| 101 | ButtplugClientEvent::ServerDisconnect => { |
| 102 | println!("[!] Server connection lost!"); |
| 103 | } |
| 104 | ButtplugClientEvent::Error(err) => { |
| 105 | println!("[!] Error: {}", err); |
| 106 | } |
| 107 | _ => {} |
| 108 | } |
| 109 | } |
| 110 | }); |
| 111 | |
| 112 | // Step 3: Connect to the server |
| 113 | println!("Connecting to Intiface Central..."); |
| 114 | let connector = ButtplugRemoteClientConnector::< |
| 115 | ButtplugWebsocketClientTransport, |
| 116 | ButtplugClientJSONSerializer, |
| 117 | >::new(ButtplugWebsocketClientTransport::new_insecure_connector( |
| 118 | "ws://127.0.0.1:12345", |
| 119 | )); |
| 120 | |
| 121 | if let Err(e) = client.connect(connector).await { |
| 122 | match e { |
| 123 | ButtplugClientError::ButtplugConnectorError(error) => { |
| 124 | println!("ERROR: Could not connect to Intiface Central!"); |
| 125 | println!("Make sure Intiface Central is running and the server is started."); |
| 126 | println!("Default address: ws://127.0.0.1:12345"); |
| 127 | println!("Error: {}", error); |
| 128 | return Ok(()); |
| 129 | } |
| 130 | _ => return Err(e.into()), |
| 131 | } |
| 132 | } |
| 133 | println!("Connected!\n"); |
| 134 | |
| 135 | // Step 4: Scan for devices |
| 136 | println!("Scanning for devices..."); |
nothing calls this directly
no test coverage detected