diff options
author | Henauxg <19689618+Henauxg@users.noreply.github.com> | 2022-10-26 16:16:45 +0200 |
---|---|---|
committer | Henauxg <19689618+Henauxg@users.noreply.github.com> | 2022-10-26 16:16:45 +0200 |
commit | 4e747425b4eb96bb3d6f5914ea7dff15a2fa30d4 (patch) | |
tree | f8ad04666ce4d2855769ec4afdaa7c63149b3d3b | |
parent | a899cf6700a1b7bee0517162801f28df3538ab32 (diff) |
[example] Update terminal_chat_client to use the new connection API and ConnectionEvent
-rw-r--r-- | examples/terminal_chat_client/main.rs | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/examples/terminal_chat_client/main.rs b/examples/terminal_chat_client/main.rs index e5ca98a..a9903d4 100644 --- a/examples/terminal_chat_client/main.rs +++ b/examples/terminal_chat_client/main.rs @@ -10,7 +10,10 @@ use bevy::{ prelude::{info, warn, App, Commands, CoreStage, EventReader, EventWriter, Res, ResMut}, }; use bevy_quinnet::{ - client::{Client, ClientConfigurationData, QuinnetClientPlugin}, + client::{ + CertificateVerificationMode, Client, ClientConfigurationData, ConnectionEvent, + QuinnetClientPlugin, + }, ClientId, }; use chat_protocol::{ClientMessage, ServerMessage}; @@ -102,21 +105,34 @@ fn start_terminal_listener(mut commands: Commands) { } fn start_connection(client: ResMut<Client>) { - client.connect().unwrap(); + client + .connect( + ClientConfigurationData::new("127.0.0.1".to_string(), 6000, "0.0.0.0".to_string(), 0), + CertificateVerificationMode::SkipVerification, + ) + .unwrap(); - let username: String = rand::thread_rng() - .sample_iter(&Alphanumeric) - .take(7) - .map(char::from) - .collect(); + // 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() +} - println!("--- Joining with name: {}", username); - println!("--- Type 'quit' to disconnect"); +fn handle_server_events(connection_events: EventReader<ConnectionEvent>, client: ResMut<Client>) { + if !connection_events.is_empty() { + // We are connected + let username: String = rand::thread_rng() + .sample_iter(&Alphanumeric) + .take(7) + .map(char::from) + .collect(); - // You can already send message(s) even before being connected, they will be buffered. Else, just wait for client.is_connected() - client - .send_message(ClientMessage::Join { name: username }) - .unwrap(); + println!("--- Joining with name: {}", username); + println!("--- Type 'quit' to disconnect"); + + client + .send_message(ClientMessage::Join { name: username }) + .unwrap(); + + connection_events.clear(); + } } fn main() { @@ -124,18 +140,12 @@ fn main() { .add_plugin(ScheduleRunnerPlugin::default()) .add_plugin(LogPlugin::default()) .add_plugin(QuinnetClientPlugin::default()) - // Currently, bevy_quinnet takes its configuration as a resource - .insert_resource(ClientConfigurationData::new( - "127.0.0.1".to_string(), - 6000, - "0.0.0.0".to_string(), - 0, - )) .insert_resource(Users::default()) .add_startup_system(start_terminal_listener) .add_startup_system(start_connection) .add_system(handle_terminal_messages) .add_system(handle_server_messages) + .add_system(handle_server_events) // CoreStage::PostUpdate so that AppExit events generated in the previous stage are available .add_system_to_stage(CoreStage::PostUpdate, on_app_exit) .run(); |