-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (98 loc) · 3.57 KB
/
Copy pathProgram.cs
File metadata and controls
117 lines (98 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Buttplug C# - Async Patterns Example
//
// This example demonstrates async/await patterns and event handling
// in the Buttplug C# library. The library is fully async - all operations
// that might block (network, device communication) use async/await.
using Buttplug.Client;
var client = new ButtplugClient("Async Example");
// Events in C# use the standard EventHandler pattern.
// Handlers receive (object sender, EventArgs args).
// DeviceAdded is fired when a new device connects
client.DeviceAdded += async (sender, args) =>
{
// Note: Event handlers can be async!
// The device is available via args.Device
Console.WriteLine($"[Event] Device added: {args.Device.Name}");
// You can interact with the device in the event handler
if (args.Device.HasOutput(Buttplug.Core.Messages.OutputType.Vibrate))
{
Console.WriteLine($" Sending welcome vibration...");
await args.Device.RunOutputAsync(DeviceOutput.Vibrate.Percent(0.25));
await Task.Delay(200);
await args.Device.StopAsync();
}
};
// DeviceRemoved is fired when a device disconnects
client.DeviceRemoved += (sender, args) =>
{
Console.WriteLine($"[Event] Device removed: {args.Device.Name}");
};
// ScanningFinished is fired when scanning completes
// (some protocols scan continuously until stopped)
client.ScanningFinished += (sender, args) =>
{
Console.WriteLine("[Event] Scanning finished");
};
// ErrorReceived is fired for asynchronous errors
// (errors not directly caused by a method call you awaited)
client.ErrorReceived += (sender, args) =>
{
Console.WriteLine($"[Event] Error: {args.Exception.Message}");
};
// ServerDisconnect is fired when the server connection drops
client.ServerDisconnect += (sender, args) =>
{
Console.WriteLine("[Event] Server disconnected!");
};
// PingTimeout is fired if the server doesn't respond to keep-alive pings
client.PingTimeout += (sender, args) =>
{
Console.WriteLine("[Event] Server ping timeout!");
};
// InputReadingReceived is fired when subscribed sensor data arrives
client.InputReadingReceived += (sender, args) =>
{
Console.WriteLine($"[Event] Input reading from device {args.DeviceIndex}: {args.Reading}");
};
// Connect asynchronously - this may take time due to network
await client.ConnectAsync("ws://127.0.0.1:12345");
// Scanning is also async - we start it and wait for events
Console.WriteLine("Turn on devices now (events will be printed)...\n");
await client.StartScanningAsync();
// Use CancellationToken for timeouts and cancellation
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
try
{
// Wait for user input or timeout
Console.WriteLine("Press Enter to stop scanning (or wait 10 seconds)...");
await Task.Run(() => Console.ReadLine(), cts.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("Scan timeout reached.");
}
await client.StopScanningAsync();
// Demonstrate concurrent operations
var devices = client.Devices;
if (devices.Length > 0)
{
// Send commands to all devices concurrently
var tasks = devices
.Where(d => d.HasOutput(Buttplug.Core.Messages.OutputType.Vibrate))
.Select(async device =>
{
await device.RunOutputAsync(DeviceOutput.Vibrate.Percent(0.5));
await Task.Delay(500);
await device.StopAsync();
});
// Wait for all commands to complete
await Task.WhenAll(tasks);
}
else
{
Console.WriteLine("No devices connected.");
}
Console.WriteLine("\nPress Enter to disconnect...");
Console.ReadLine();
await client.DisconnectAsync();
Console.WriteLine("Disconnected.");