|
5 | 5 | [](https://docs.rs/bevy_http_client) |
6 | 6 | [](https://github.com/Seldom-SE/seldom_pixel#license) |
7 | 7 |
|
8 | | -A simple HTTP client Bevy Plugin for both native and WASM. |
| 8 | +A simple HTTP client Bevy Plugin for both native and WASM. |
9 | 9 |
|
10 | 10 | ## Example |
11 | 11 |
|
12 | 12 | ```rust |
13 | 13 | use bevy::{prelude::*, time::common_conditions::on_timer}; |
14 | 14 | use bevy_http_client::prelude::*; |
| 15 | +use serde::Deserialize; |
| 16 | + |
| 17 | +#[derive(Debug, Clone, Deserialize, Default)] |
| 18 | +pub struct IpInfo { |
| 19 | + pub ip: String, |
| 20 | +} |
15 | 21 |
|
16 | 22 | fn main() { |
17 | | - App::new() |
18 | | - .add_plugins((MinimalPlugins, HttpClientPlugin)) |
| 23 | + let mut app = App::new(); |
| 24 | + app.add_plugins((MinimalPlugins, HttpClientPlugin)) |
19 | 25 | .add_systems(Update, handle_response) |
20 | 26 | .add_systems( |
21 | 27 | Update, |
22 | 28 | send_request.run_if(on_timer(std::time::Duration::from_secs(1))), |
23 | | - ) |
24 | | - .run() |
| 29 | + ); |
| 30 | + app.register_request_type::<IpInfo>(); |
| 31 | + app.run(); |
25 | 32 | } |
26 | 33 |
|
27 | | -fn send_request(mut commands: Commands) { |
28 | | - let req = ehttp::Request::get("https://api.ipify.org?format=json"); |
29 | | - commands.spawn(HttpRequest(req)); |
| 34 | +fn send_request(mut ev_request: EventWriter<TypedRequest<IpInfo>>) { |
| 35 | + ev_request.send( |
| 36 | + HttpClient::new() |
| 37 | + .get("https://api.ipify.org?format=json") |
| 38 | + .with_type::<IpInfo>(), |
| 39 | + ); |
30 | 40 | } |
31 | 41 |
|
32 | | -fn handle_response(mut commands: Commands, responses: Query<(Entity, &HttpResponse)>) { |
33 | | - for (entity, response) in responses.iter() { |
34 | | - println!("response: {:?}", response.text()); |
35 | | - commands.entity(entity).despawn_recursive(); |
| 42 | +fn handle_response(mut ev_response: EventReader<TypedResponse<IpInfo>>) { |
| 43 | + for response in ev_response.read() { |
| 44 | + println!("ip: {}", response.ip); |
36 | 45 | } |
37 | 46 | } |
38 | 47 | ``` |
39 | 48 |
|
40 | | - |
41 | 49 | ## Supported Versions |
42 | 50 |
|
43 | | -| bevy | bevy_cronjob | |
44 | | -|------|--------------| |
45 | | -| 0.13 | 0.4 | |
46 | | -| 0.12 | 0.3 | |
47 | | -| 0.11 | 0.1 | |
| 51 | +| bevy | bevy_http_client | |
| 52 | +|------|------------------| |
| 53 | +| 0.13 | 0.4, 0,5 | |
| 54 | +| 0.12 | 0.3 | |
| 55 | +| 0.11 | 0.1 | |
48 | 56 |
|
49 | 57 | ## License |
50 | 58 |
|
|
0 commit comments