#![type_length_limit = "5500000"]
use async_std::task;
use buttplug::client::{
connectors::websocket::ButtplugWebsocketClientConnector, device::VibrateCommand,
ButtplugClient, ButtplugClientEvent,
};
use std::time::Duration;
async fn device_control_example() {
let connector = ButtplugWebsocketClientConnector::new("ws://localhost:12345", true);
let app_closure = |mut client: ButtplugClient| {
async move {
if let Err(err) = client.start_scanning().await {
println!("Client errored when starting scan! {}", err);
return;
}
let mut device = None;
loop {
match client.wait_for_event().await {
Ok(event) => match event {
ButtplugClientEvent::DeviceAdded(dev) => {
println!("We got a device: {}", dev.name);
device = Some(dev);
break;
}
ButtplugClientEvent::ServerDisconnect => {
println!("Server disconnected!");
break;
}
_ => {
println!("Got some other kind of event we don't care about");
}
},
Err(err) => {
println!("Error while waiting for client events: {}", err);
break;
}
}
}
if let Some(mut dev) = device {
if dev.allowed_messages.contains_key("VibrateCmd") {
dev.vibrate(VibrateCommand::Speed(1.0)).await.unwrap();
println!("{} should start vibrating!", dev.name);
task::sleep(Duration::from_secs(1)).await;
dev.stop().await.unwrap();
println!("{} should stop vibrating!", dev.name);
task::sleep(Duration::from_secs(1)).await;
} else {
println!("{} doesn't vibrate! This example should be updated to handle rotation and linear movement!", dev.name);
}
}
println!("Exiting example");
}
};
ButtplugClient::run("Example Client", connector, app_closure)
.await
.unwrap();
}
fn main() {
task::block_on(async {
device_control_example().await;
});
}