Add unsubscribe to objects

This commit is contained in:
Mihai Dinculescu
2022-10-18 21:46:01 +01:00
parent a2002e02cd
commit 0db9847030
11 changed files with 211 additions and 109 deletions

View File

@@ -26,27 +26,40 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = SimConnect::new("Simple Program");
match client {
Ok(mut client) => loop {
let notification = client.get_next_dispatch()?;
Ok(mut client) => {
let mut notifications_received = 0;
match notification {
Some(Notification::Open) => {
println!("Open");
loop {
let notification = client.get_next_dispatch()?;
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
}
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
println!("{gps_data:?}");
match notification {
Some(Notification::Open) => {
println!("Connection opened.");
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
}
}
_ => (),
}
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
println!("{gps_data:?}");
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
},
notifications_received += 1;
// After we have received 10 notifications, we unregister the struct
if notifications_received > 10 {
client.unregister_object::<GpsData>()?;
println!("Subscription stopped.");
break;
}
}
}
_ => (),
}
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
}
}
Err(e) => {
println!("Error: {e:?}")
}
@@ -56,4 +69,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
```
See [more examples](https://github.com/mihai-dinculescu/simconnect-sdk/tree/main/examples).
See [more examples][examples].
[examples]: https://github.com/mihai-dinculescu/simconnect-sdk/tree/main/examples