## Objective Add a way to stream BRP requests when the data changes. ## Solution #### BRP Side (reusable for other transports) Add a new method handler type that returns a optional value. This handler is run in update and if a value is returned it will be sent on the message channel. Custom watching handlers can be added with `RemotePlugin::with_watching_method`. #### HTTP Side If a request comes in with `+watch` in the method, it will respond with `text/event-stream` rather than a single response. ## Testing I tested with the podman HTTP client. This client has good support for SSE's if you want to test it too. ## Parts I want some opinions on - For separating watching methods I chose to add a `+watch` suffix to the end kind of like `content-type` headers. A get would be `bevy/get+watch`. - Should watching methods send an initial response with everything or only respond when a change happens? Currently the later is what happens. ## Future work - The `bevy/query` method would also benefit from this but that condition will be quite complex so I will leave that to later. --------- Co-authored-by: Zachary Harrold <zac@harrold.com.au>
		
			
				
	
	
		
			72 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
//! A Bevy app that you can connect to with the BRP and edit.
 | 
						|
 | 
						|
use bevy::math::ops::cos;
 | 
						|
use bevy::{
 | 
						|
    input::common_conditions::input_just_pressed,
 | 
						|
    prelude::*,
 | 
						|
    remote::{http::RemoteHttpPlugin, RemotePlugin},
 | 
						|
};
 | 
						|
use serde::{Deserialize, Serialize};
 | 
						|
 | 
						|
fn main() {
 | 
						|
    App::new()
 | 
						|
        .add_plugins(DefaultPlugins)
 | 
						|
        .add_plugins(RemotePlugin::default())
 | 
						|
        .add_plugins(RemoteHttpPlugin::default())
 | 
						|
        .add_systems(Startup, setup)
 | 
						|
        .add_systems(Update, remove.run_if(input_just_pressed(KeyCode::Space)))
 | 
						|
        .add_systems(Update, move_cube)
 | 
						|
        .register_type::<Cube>()
 | 
						|
        .run();
 | 
						|
}
 | 
						|
 | 
						|
fn setup(
 | 
						|
    mut commands: Commands,
 | 
						|
    mut meshes: ResMut<Assets<Mesh>>,
 | 
						|
    mut materials: ResMut<Assets<StandardMaterial>>,
 | 
						|
) {
 | 
						|
    // circular base
 | 
						|
    commands.spawn((
 | 
						|
        Mesh3d(meshes.add(Circle::new(4.0))),
 | 
						|
        MeshMaterial3d(materials.add(Color::WHITE)),
 | 
						|
        Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
 | 
						|
    ));
 | 
						|
 | 
						|
    // cube
 | 
						|
    commands.spawn((
 | 
						|
        Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
 | 
						|
        MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
 | 
						|
        Transform::from_xyz(0.0, 0.5, 0.0),
 | 
						|
        Cube(1.0),
 | 
						|
    ));
 | 
						|
 | 
						|
    // light
 | 
						|
    commands.spawn((
 | 
						|
        PointLight {
 | 
						|
            shadows_enabled: true,
 | 
						|
            ..default()
 | 
						|
        },
 | 
						|
        Transform::from_xyz(4.0, 8.0, 4.0),
 | 
						|
    ));
 | 
						|
 | 
						|
    // camera
 | 
						|
    commands.spawn((
 | 
						|
        Camera3d::default(),
 | 
						|
        Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
 | 
						|
    ));
 | 
						|
}
 | 
						|
 | 
						|
fn move_cube(mut query: Query<&mut Transform, With<Cube>>, time: Res<Time>) {
 | 
						|
    for mut transform in &mut query {
 | 
						|
        transform.translation.y = -cos(time.elapsed_seconds()) + 1.5;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
fn remove(mut commands: Commands, query: Query<Entity, With<Cube>>) {
 | 
						|
    commands.entity(query.single()).remove::<Cube>();
 | 
						|
}
 | 
						|
 | 
						|
#[derive(Component, Reflect, Serialize, Deserialize)]
 | 
						|
#[reflect(Component, Serialize, Deserialize)]
 | 
						|
struct Cube(f32);
 |