MCPcopy Create free account
hub / github.com/buttplugio/buttplug / main

Function main

examples/src/bin/device_enumeration.rs:20–79  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 9

spawnFunction · 0.85
OkClass · 0.85
wait_for_inputFunction · 0.70
connectMethod · 0.45
event_streamMethod · 0.45
start_scanningMethod · 0.45
stop_scanningMethod · 0.45
devicesMethod · 0.45
disconnectMethod · 0.45

Tested by

no test coverage detected