Buttplug sex toy control library
1use buttplug_client::{
2 ButtplugClient,
3 ButtplugClientError,
4 connector::ButtplugRemoteClientConnector,
5 serializer::ButtplugClientJSONSerializer,
6};
7
8use buttplug_core::errors::ButtplugError;
9use buttplug_transport_websocket_tungstenite::ButtplugWebsocketClientTransport;
10use tokio::io::{self, AsyncBufReadExt, BufReader};
11
12async fn wait_for_input() {
13 BufReader::new(io::stdin())
14 .lines()
15 .next_line()
16 .await
17 .unwrap();
18}
19
20#[tokio::main]
21async fn main() -> anyhow::Result<()> {
22 // After you've created a connector, the connection looks the same no
23 // matter what, though the errors thrown may be different.
24 let connector = ButtplugRemoteClientConnector::<
25 ButtplugWebsocketClientTransport,
26 ButtplugClientJSONSerializer,
27 >::new(ButtplugWebsocketClientTransport::new_insecure_connector(
28 "ws://127.0.0.1:12345",
29 ));
30
31 // Now we connect. If anything goes wrong here, we'll get an Err with either
32 //
33 // - A ButtplugClientConnectionError if there's a problem with
34 // the Connector, like the network address being wrong, server not
35 // being up, etc.
36 // - A ButtplugHandshakeError if there is a client/server version
37 // mismatch.
38 let client = ButtplugClient::new("Example Client");
39 if let Err(e) = client.connect(connector).await {
40 match e {
41 ButtplugClientError::ButtplugConnectorError(error) => {
42 // If our connection failed, because the server wasn't turned on,
43 // SSL/TLS wasn't turned off, etc, we'll just print and exit
44 // here.
45 println!("Can't connect, exiting! Message: {}", error);
46 wait_for_input().await;
47 return Ok(());
48 }
49 ButtplugClientError::ButtplugError(error) => match error {
50 ButtplugError::ButtplugHandshakeError(error) => {
51 // This means our client is newer than our server, and we need to
52 // upgrade the server we're connecting to.
53 println!("Handshake issue, exiting! Message: {}", error);
54 wait_for_input().await;
55 return Ok(());
56 }
57 error => {
58 println!("Unexpected error type! {}", error);
59 wait_for_input().await;
60 return Ok(());
61 }
62 },
63 _ => {
64 // None of the other errors are valid in this instance.
65 }
66 }
67 };
68
69 // We're connected, yay!
70 println!("Connected! Check Server for Client Name.");
71
72 wait_for_input().await;
73
74 // And now we disconnect as usual
75 client.disconnect().await?;
76
77 Ok(())
78}