Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/remote/client.rs
6592 views
1
//! A simple command line client that allows issuing queries to a remote Bevy
2
//! app via the BRP.
3
//! This example requires the `bevy_remote` feature to be enabled.
4
//! You can run it with the following command:
5
//! ```text
6
//! cargo run --example client --features="bevy_remote"
7
//! ```
8
//! This example assumes that the `server` example is running on the same machine.
9
10
use std::any::type_name;
11
12
use anyhow::Result as AnyhowResult;
13
use bevy::{
14
ecs::hierarchy::ChildOf,
15
prelude::info,
16
remote::{
17
builtin_methods::{
18
BrpQuery, BrpQueryFilter, BrpQueryParams, ComponentSelector, BRP_QUERY_METHOD,
19
},
20
http::{DEFAULT_ADDR, DEFAULT_PORT},
21
BrpRequest,
22
},
23
transform::components::Transform,
24
};
25
26
/// The application entry point.
27
fn main() -> AnyhowResult<()> {
28
// Create the URL. We're going to need it to issue the HTTP request.
29
let host_part = format!("{DEFAULT_ADDR}:{DEFAULT_PORT}");
30
let url = format!("http://{host_part}/");
31
// Creates a request to get all Transform components from the remote Bevy app.
32
// This request will return all entities that have a Transform component.
33
run_transform_only_query(&url)?;
34
35
// Create a query that only returns root entities - ie, entities that do not
36
// have a parent.
37
run_query_root_entities(&url)?;
38
39
// Create a query all request to send to the remote Bevy app.
40
// This request will return all entities in the app, their components, and their
41
// component values.
42
run_query_all_components_and_entities(&url)?;
43
44
Ok(())
45
}
46
47
fn run_query_all_components_and_entities(url: &str) -> Result<(), anyhow::Error> {
48
let query_all_req = BrpRequest {
49
jsonrpc: String::from("2.0"),
50
method: String::from(BRP_QUERY_METHOD),
51
id: Some(serde_json::to_value(1)?),
52
params: Some(
53
serde_json::to_value(BrpQueryParams {
54
data: BrpQuery {
55
components: Vec::default(),
56
option: ComponentSelector::All,
57
has: Vec::default(),
58
},
59
strict: false,
60
filter: BrpQueryFilter::default(),
61
})
62
.expect("Unable to convert query parameters to a valid JSON value"),
63
),
64
};
65
info!("query_all req: {query_all_req:#?}");
66
let query_all_res = ureq::post(url)
67
.send_json(query_all_req)?
68
.body_mut()
69
.read_json::<serde_json::Value>()?;
70
info!("{query_all_res:#}");
71
Ok(())
72
}
73
74
fn run_transform_only_query(url: &str) -> Result<(), anyhow::Error> {
75
let get_transform_request = BrpRequest {
76
jsonrpc: String::from("2.0"),
77
method: String::from(BRP_QUERY_METHOD),
78
id: Some(serde_json::to_value(1)?),
79
params: Some(
80
serde_json::to_value(BrpQueryParams {
81
data: BrpQuery {
82
components: vec![type_name::<Transform>().to_string()],
83
..Default::default()
84
},
85
strict: false,
86
filter: BrpQueryFilter::default(),
87
})
88
.expect("Unable to convert query parameters to a valid JSON value"),
89
),
90
};
91
info!("transform request: {get_transform_request:#?}");
92
let res = ureq::post(url)
93
.send_json(get_transform_request)?
94
.body_mut()
95
.read_json::<serde_json::Value>()?;
96
info!("{res:#}");
97
Ok(())
98
}
99
100
fn run_query_root_entities(url: &str) -> Result<(), anyhow::Error> {
101
let get_transform_request = BrpRequest {
102
jsonrpc: String::from("2.0"),
103
method: String::from(BRP_QUERY_METHOD),
104
id: Some(serde_json::to_value(1)?),
105
params: Some(
106
serde_json::to_value(BrpQueryParams {
107
data: BrpQuery {
108
components: Vec::default(),
109
option: ComponentSelector::All,
110
has: Vec::default(),
111
},
112
strict: false,
113
filter: BrpQueryFilter {
114
without: vec![type_name::<ChildOf>().to_string()],
115
with: Vec::default(),
116
},
117
})
118
.expect("Unable to convert query parameters to a valid JSON value"),
119
),
120
};
121
info!("transform request: {get_transform_request:#?}");
122
let res = ureq::post(url)
123
.send_json(get_transform_request)?
124
.body_mut()
125
.read_json::<serde_json::Value>()?;
126
info!("{res:#}");
127
Ok(())
128
}
129
130