Buttplug sex toy control library
1use buttplug_client::{
2 ButtplugClient,
3 ButtplugClientEvent,
4 connector::ButtplugRemoteClientConnector,
5 serializer::ButtplugClientJSONSerializer,
6};
7use buttplug_transport_websocket_tungstenite::ButtplugWebsocketClientTransport;
8use futures::StreamExt;
9use tokio::io::{self, AsyncBufReadExt, BufReader};
10
11async fn wait_for_input() {
12 BufReader::new(io::stdin())
13 .lines()
14 .next_line()
15 .await
16 .unwrap();
17}
18
19#[tokio::main]
20async fn main() -> anyhow::Result<()> {
21 // Usual embedded connector setup. We'll assume the server found all
22 // of the subtype managers for us (the default features include all of them).
23 //let client = in_process_client("Example Client", false).await;
24 // To create a Websocket Connector, you need the websocket address and some generics fuckery.
25 let connector = ButtplugRemoteClientConnector::<
26 ButtplugWebsocketClientTransport,
27 ButtplugClientJSONSerializer,
28 >::new(ButtplugWebsocketClientTransport::new_insecure_connector(
29 "ws://127.0.0.1:12345",
30 ));
31 let client = ButtplugClient::new("Example Client");
32 client.connect(connector).await?;
33 let mut events = client.event_stream();
34
35 // Set up our DeviceAdded/DeviceRemoved/ScanningFinished event handlers before connecting.
36 tokio::spawn(async move {
37 while let Some(event) = events.next().await {
38 match event {
39 ButtplugClientEvent::DeviceAdded(device) => {
40 println!("Device {} Connected!", device.name());
41 }
42 ButtplugClientEvent::DeviceRemoved(info) => {
43 println!("Device {} Removed!", info.name());
44 }
45 ButtplugClientEvent::ScanningFinished => {
46 println!("Device scanning is finished!");
47 }
48 _ => {}
49 }
50 }
51 });
52
53 // We're connected, yay!
54 println!("Connected!");
55
56 // Now we can start scanning for devices, and any time a device is
57 // found, we should see the device name printed out.
58 client.start_scanning().await?;
59 wait_for_input().await;
60
61 // Some Subtype Managers will scan until we still them to stop, so
62 // let's stop them now.
63 client.stop_scanning().await?;
64 wait_for_input().await;
65
66 // Since we've scanned, the client holds information about devices it
67 // knows about for us. These devices can be accessed with the Devices
68 // getter on the client.
69 println!("Client currently knows about these devices:");
70 for (_, device) in client.devices() {
71 println!("- {}", device.name());
72 }
73 wait_for_input().await;
74
75 // And now we disconnect as usual.
76 client.disconnect().await?;
77
78 Ok(())
79}