diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/breakout/client.rs | 37 | ||||
-rw-r--r-- | examples/chat/client.rs | 53 | ||||
-rw-r--r-- | examples/chat/server.rs | 6 |
3 files changed, 58 insertions, 38 deletions
diff --git a/examples/breakout/client.rs b/examples/breakout/client.rs index eb7a8bb..ecc694d 100644 --- a/examples/breakout/client.rs +++ b/examples/breakout/client.rs @@ -15,7 +15,9 @@ use bevy::{ }, }; use bevy_quinnet::{ - client::{certificate::CertificateVerificationMode, Client, ClientConfigurationData}, + client::{ + certificate::CertificateVerificationMode, Client, Connection, ConnectionConfiguration, + }, ClientId, }; @@ -90,18 +92,17 @@ struct WallBundle { sprite_bundle: SpriteBundle, } -pub(crate) fn start_connection(client: ResMut<Client>) { - client - .connect( - ClientConfigurationData::new( - SERVER_HOST.to_string(), - SERVER_PORT, - "0.0.0.0".to_string(), - 0, - ), - CertificateVerificationMode::SkipVerification, - ) - .unwrap(); +pub(crate) fn start_connection(client: ResMut<Client>, mut commands: Commands) { + client.spawn_connection( + &mut commands, + ConnectionConfiguration::new( + SERVER_HOST.to_string(), + SERVER_PORT, + "0.0.0.0".to_string(), + 0, + ), + CertificateVerificationMode::SkipVerification, + ); } fn spawn_paddle(commands: &mut Commands, position: &Vec3, owned: bool) -> Entity { @@ -189,7 +190,7 @@ pub(crate) fn spawn_bricks( pub(crate) fn handle_server_messages( mut commands: Commands, - mut client: ResMut<Client>, + mut connection: Query<&mut Connection>, mut client_data: ResMut<ClientData>, mut entity_mapping: ResMut<NetworkMapping>, mut game_state: ResMut<State<GameState>>, @@ -199,7 +200,8 @@ pub(crate) fn handle_server_messages( mut scoreboard: ResMut<Scoreboard>, mut collision_events: EventWriter<CollisionEvent>, ) { - while let Ok(Some(message)) = client.receive_message::<ServerMessage>() { + let mut connection = connection.get_single_mut().unwrap(); + while let Ok(Some(message)) = connection.receive_message::<ServerMessage>() { match message { ServerMessage::InitClient { client_id } => { client_data.self_id = client_id; @@ -285,7 +287,7 @@ pub(crate) struct PaddleState { } pub(crate) fn move_paddle( - client: ResMut<Client>, + mut connection: Query<&mut Connection>, keyboard_input: Res<Input<KeyCode>>, mut local: Local<PaddleState>, ) { @@ -300,7 +302,8 @@ pub(crate) fn move_paddle( } if local.current_input != paddle_input { - client + let connection = connection.get_single_mut().unwrap(); + connection .send_message(ClientMessage::PaddleInput { input: paddle_input.clone(), }) diff --git a/examples/chat/client.rs b/examples/chat/client.rs index 15d515a..dceaf23 100644 --- a/examples/chat/client.rs +++ b/examples/chat/client.rs @@ -8,14 +8,14 @@ use bevy::{ app::{AppExit, ScheduleRunnerPlugin}, log::LogPlugin, prelude::{ - info, warn, App, Commands, CoreStage, Deref, DerefMut, EventReader, EventWriter, Res, + info, warn, App, Commands, CoreStage, Deref, DerefMut, EventReader, EventWriter, Query, ResMut, Resource, }, }; use bevy_quinnet::{ client::{ - certificate::CertificateVerificationMode, Client, ClientConfigurationData, ConnectionEvent, - QuinnetClientPlugin, + certificate::{CertificateVerificationMode, TrustOnFirstUseConfig}, + Client, Connection, ConnectionConfiguration, ConnectionEvent, QuinnetClientPlugin, }, ClientId, }; @@ -35,16 +35,20 @@ struct Users { #[derive(Resource, Deref, DerefMut)] struct TerminalReceiver(mpsc::Receiver<String>); -pub fn on_app_exit(app_exit_events: EventReader<AppExit>, client: Res<Client>) { +pub fn on_app_exit(app_exit_events: EventReader<AppExit>, mut connection: Query<&Connection>) { if !app_exit_events.is_empty() { - client.send_message(ClientMessage::Disconnect {}).unwrap(); + let connection = connection.get_single_mut().unwrap(); + connection + .send_message(ClientMessage::Disconnect {}) + .unwrap(); // TODO Clean: event to let the async client send his last messages. sleep(Duration::from_secs_f32(0.1)); } } -fn handle_server_messages(mut client: ResMut<Client>, mut users: ResMut<Users>) { - while let Ok(Some(message)) = client.receive_message::<ServerMessage>() { +fn handle_server_messages(mut users: ResMut<Users>, mut connection: Query<&mut Connection>) { + let mut connection = connection.get_single_mut().unwrap(); + while let Ok(Some(message)) = connection.receive_message::<ServerMessage>() { match message { ServerMessage::ClientConnected { client_id, @@ -81,15 +85,16 @@ fn handle_server_messages(mut client: ResMut<Client>, mut users: ResMut<Users>) } fn handle_terminal_messages( - client: ResMut<Client>, mut terminal_messages: ResMut<TerminalReceiver>, mut app_exit_events: EventWriter<AppExit>, + mut connection: Query<&Connection>, ) { + let connection = connection.get_single_mut().unwrap(); while let Ok(message) = terminal_messages.try_recv() { if message == "quit" { app_exit_events.send(AppExit); } else { - client + connection .send_message(ClientMessage::ChatMessage { message: message }) .expect("Failed to send chat message"); } @@ -110,18 +115,25 @@ fn start_terminal_listener(mut commands: Commands) { commands.insert_resource(TerminalReceiver(from_terminal_receiver)); } -fn start_connection(client: ResMut<Client>) { - client - .connect( - ClientConfigurationData::new("127.0.0.1".to_string(), 6000, "0.0.0.0".to_string(), 0), - CertificateVerificationMode::SkipVerification, - ) - .unwrap(); - - // You can already send message(s) even before being connected, they will be buffered. In this example we will wait for a ConnectionEvent. We could also check client.is_connected() +fn start_connection(mut commands: Commands, client: ResMut<Client>) { + client.spawn_connection( + &mut commands, + ConnectionConfiguration::new("127.0.0.1".to_string(), 6000, "0.0.0.0".to_string(), 0), + CertificateVerificationMode::TrustOnFirstUse(TrustOnFirstUseConfig { + known_hosts: bevy_quinnet::client::certificate::KnownHosts::HostsFile( + "my_own_hosts_file".to_string(), + ), + ..Default::default() + }), + ); + + // You can already send message(s) even before being connected, they will be buffered. In this example we will wait for a ConnectionEvent. } -fn handle_client_events(connection_events: EventReader<ConnectionEvent>, client: ResMut<Client>) { +fn handle_client_events( + connection_events: EventReader<ConnectionEvent>, + mut connection: Query<&Connection>, +) { if !connection_events.is_empty() { // We are connected let username: String = rand::thread_rng() @@ -133,7 +145,8 @@ fn handle_client_events(connection_events: EventReader<ConnectionEvent>, client: println!("--- Joining with name: {}", username); println!("--- Type 'quit' to disconnect"); - client + let connection = connection.get_single_mut().unwrap(); + connection .send_message(ClientMessage::Join { name: username }) .unwrap(); diff --git a/examples/chat/server.rs b/examples/chat/server.rs index facaefa..d9fbc02 100644 --- a/examples/chat/server.rs +++ b/examples/chat/server.rs @@ -114,7 +114,11 @@ fn start_listening(mut server: ResMut<Server>) { server .start( ServerConfigurationData::new("127.0.0.1".to_string(), 6000, "0.0.0.0".to_string()), - CertificateRetrievalMode::GenerateSelfSigned, + CertificateRetrievalMode::LoadFromFileOrGenerateSelfSigned { + cert_file: "cert.pem".to_string(), + key_file: "key.pem".to_string(), + save_on_disk: true, + }, ) .unwrap(); } |