diff options
author | Henauxg <19689618+Henauxg@users.noreply.github.com> | 2022-11-14 18:51:45 +0100 |
---|---|---|
committer | Henauxg <19689618+Henauxg@users.noreply.github.com> | 2022-11-14 18:51:45 +0100 |
commit | cb81a51ba484b44ede62e121f0d72ae87710c58c (patch) | |
tree | bf13635ba44d91cbd7194aba9d3e7b8c3a7a6e84 | |
parent | 0e6e24d4272da54147748830e9525bbb775459a0 (diff) |
[examples] Update examples to match client API
-rw-r--r-- | examples/breakout/client.rs | 37 | ||||
-rw-r--r-- | examples/chat/client.rs | 53 | ||||
-rw-r--r-- | examples/chat/server.rs | 8 |
3 files changed, 59 insertions, 39 deletions
diff --git a/examples/breakout/client.rs b/examples/breakout/client.rs index 9546b26..43f5b0a 100644 --- a/examples/breakout/client.rs +++ b/examples/breakout/client.rs @@ -14,7 +14,9 @@ use bevy::{ }, }; use bevy_quinnet::{ - client::{certificate::CertificateVerificationMode, Client, ClientConfigurationData}, + client::{ + certificate::CertificateVerificationMode, Client, Connection, ConnectionConfiguration, + }, ClientId, }; @@ -88,18 +90,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 { @@ -184,7 +185,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>>, @@ -194,7 +195,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; @@ -280,7 +282,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>, ) { @@ -295,7 +297,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 0e6fad7..da01dfb 100644 --- a/examples/chat/client.rs +++ b/examples/chat/client.rs @@ -7,12 +7,12 @@ use std::{ use bevy::{ app::{AppExit, ScheduleRunnerPlugin}, log::LogPlugin, - prelude::{info, warn, App, Commands, CoreStage, EventReader, EventWriter, Res, ResMut}, + prelude::{info, warn, App, Commands, CoreStage, EventReader, EventWriter, Query, ResMut}, }; use bevy_quinnet::{ client::{ - certificate::CertificateVerificationMode, Client, ClientConfigurationData, ConnectionEvent, - QuinnetClientPlugin, + certificate::{CertificateVerificationMode, TrustOnFirstUseConfig}, + Client, Connection, ConnectionConfiguration, ConnectionEvent, QuinnetClientPlugin, }, ClientId, }; @@ -29,16 +29,20 @@ struct Users { names: HashMap<ClientId, 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, @@ -75,15 +79,16 @@ fn handle_server_messages(mut client: ResMut<Client>, mut users: ResMut<Users>) } fn handle_terminal_messages( - client: ResMut<Client>, mut terminal_messages: ResMut<mpsc::Receiver<String>>, 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"); } @@ -104,18 +109,25 @@ fn start_terminal_listener(mut commands: Commands) { commands.insert_resource(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() @@ -127,7 +139,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 26fa75e..e663fe2 100644 --- a/examples/chat/server.rs +++ b/examples/chat/server.rs @@ -54,7 +54,7 @@ fn handle_client_messages(mut server: ResMut<Server>, mut users: ResMut<Users>) } ClientMessage::Disconnect {} => { // We tell the server to disconnect this user - server.disconnect_client(client_id); + server.disconnect_client(client_id).unwrap(); handle_disconnect(&mut server, &mut users, client_id); } ClientMessage::ChatMessage { message } => { @@ -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(); } |