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

Function main

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

Source from the content-addressed store, hash-verified

77
78#[tokio::main]
79async 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...");

Callers

nothing calls this directly

Calls 15

spawnFunction · 0.85
OkClass · 0.85
read_lineFunction · 0.85
output_availableMethod · 0.80
input_availableMethod · 0.80
wait_for_inputFunction · 0.70
event_streamMethod · 0.45
connectMethod · 0.45
start_scanningMethod · 0.45
stop_scanningMethod · 0.45
devicesMethod · 0.45

Tested by

no test coverage detected