Skip to content

Commit

Permalink
Merge pull request #177 from cBournhonesque/cb/listen-server
Browse files Browse the repository at this point in the history
update all examples to support listen-server mode
  • Loading branch information
cBournhonesque committed Mar 13, 2024
2 parents 28a18db + ebfc1d9 commit 42eb069
Show file tree
Hide file tree
Showing 60 changed files with 1,615 additions and 1,041 deletions.
1 change: 1 addition & 0 deletions examples/bullet_prespawn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ metrics-exporter-prometheus = { version = "0.13.0", optional = true }
bevy-inspector-egui = "0.23"
cfg-if = "1.0.0"
ron = "0.8.1"
crossbeam-channel = "0.5.11"
35 changes: 27 additions & 8 deletions examples/bullet_prespawn/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
# Features


This example showcases how prespawning player objects on the client side works:
- you just have to add a `PreSpawnedPlayedObject` component to the pre-spawned entity. The system that spawns the entity can be identical in the client and the server

- you just have to add a `PreSpawnedPlayedObject` component to the pre-spawned entity. The system that spawns the entity
can be identical in the client and the server
- the client spawns the entity immediately in the predicted timeline
- when the client receives the server entity, it will match it with the existing pre-spawned entity!

https://github.com/cBournhonesque/lightyear/assets/8112632/ee547c32-1f14-4bdc-9e6d-67f900af84d0

## Running the example

https://github.com/cBournhonesque/lightyear/assets/8112632/ee547c32-1f14-4bdc-9e6d-67f900af84d0
You can either run the example as a "Listen Server" (the program acts as both client and server)
with: `cargo run -- listen-server`
or as dedicated server with `cargo run -- server`

Then you can launch multiple clients with the commands:

- `cargo run -- client -c 1`
- `cargo run -- client -c 2`

You can modify the file `assets/settings.ron` to modify some networking settings.

### Testing webtransport in wasm

NOTE: I am using [trunk](https://trunkrs.dev/) to build and serve the wasm example.

To test the example in wasm, you can run the following commands: `trunk serve`

# Usage
You will need a valid SSL certificate to test the example in wasm using webtransport. You will need to run the following
commands:

- Run the server with: `cargo run -- server --headless`
- Run the clients with:
`cargo run -- client -c 1`
`cargo run -- client -c 2`
- `sh examples/generate.sh` (to generate the temporary SSL certificates, they are only valid for 2 weeks)
- `cargo run -- server` to start the server. The server will print out the certificate digest (something
like `1fd28860bd2010067cee636a64bcbb492142295b297fd8c480e604b70ce4d644`)
- You then have to replace the certificate digest in the `assets/settings.ron` file with the one that the server printed
out.
- then start the client wasm test with `trunk serve`
30 changes: 9 additions & 21 deletions examples/bullet_prespawn/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::protocol::*;
use crate::shared::{color_from_id, shared_config, shared_player_movement};
use crate::{shared, ClientTransports, SharedSettings, KEY, PROTOCOL_ID};
use std::net::{Ipv4Addr, SocketAddr};
use std::str::FromStr;

use bevy::app::PluginGroupBuilder;
use bevy::ecs::schedule::{LogLevel, ScheduleBuildSettings};
use bevy::prelude::*;
Expand All @@ -11,12 +11,15 @@ use leafwing_input_manager::buttonlike::ButtonState::Pressed;
use leafwing_input_manager::orientation::Orientation;
use leafwing_input_manager::plugin::InputManagerSystem;
use leafwing_input_manager::prelude::*;

use lightyear::inputs::native::input_buffer::InputBuffer;
use lightyear::prelude::client::LeafwingInputPlugin;
use lightyear::prelude::client::*;
use lightyear::prelude::*;
use std::net::{Ipv4Addr, SocketAddr};
use std::str::FromStr;

use crate::protocol::*;
use crate::shared::{color_from_id, shared_config, shared_player_movement};
use crate::{shared, ClientTransports, SharedSettings};

pub const INPUT_DELAY_TICKS: u16 = 0;
pub const CORRECTION_TICKS_FACTOR: f32 = 1.5;
Expand All @@ -28,25 +31,10 @@ pub struct ClientPluginGroup {
impl ClientPluginGroup {
pub(crate) fn new(
client_id: u64,
client_port: u16,
server_addr: SocketAddr,
transport: ClientTransports,
transport_config: TransportConfig,
shared_settings: SharedSettings,
) -> ClientPluginGroup {
let client_addr = SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), client_port);
let transport_config = match transport {
#[cfg(not(target_family = "wasm"))]
ClientTransports::Udp => TransportConfig::UdpSocket(client_addr),
ClientTransports::WebTransport { certificate_digest } => {
TransportConfig::WebTransportClient {
client_addr,
server_addr,
#[cfg(target_family = "wasm")]
certificate_digest,
}
}
ClientTransports::WebSocket => TransportConfig::WebSocketClient { server_addr },
};
let auth = Authentication::Manual {
server_addr,
client_id,
Expand Down

0 comments on commit 42eb069

Please sign in to comment.