Skip to content

Commit

Permalink
BevyRenet: update README
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspoffo authored Jul 25, 2022
1 parent 554968c commit cc8c9db
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions bevy_renet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,88 @@
A Bevy Plugin for the [renet](https://github.com/lucaspoffo/renet) crate.
A network crate for Server/Client with cryptographically secure authentication and encypted packets.
Designed for fast paced competitive multiplayer games.

## Usage
Bevy renet is a small layer over the `renet` crate, it adds systems to call the client/server `update`and `send_packets`. `RenetClient` and `RenetServer` need to be added as a resource, so the setup is similar to `renet` itself:

#### Server
```rust
let mut app = App::new();
app.add_plugin(RenetServerPlugin);

let server = RenetServer::new(...);
app.insert_resource(server);

app.add_system(send_message_system);
app.add_system(receive_message_system);
app.add_system(handle_events_system);

// Systems

fn send_message_system(mut server: ResMut<RenetServer>) {
let channel_id = 0;
// Send a text message for all clients
server.broadcast_message(channel_id, "server message".as_bytes().to_vec());
}

fn receive_message_system(mut server: ResMut<RenetServer>) {
let channel_id = 0;
// Send a text message for all clients
for client_id in server.clients_id().into_iter() {
while let Some(message) = server.receive_message(client_id, channel_id) {
// Handle received message
}
}
}

fn handle_events_system(mut server_events: EventReader<ServerEvent>) {
while let Some(event) = server.get_event() {
for event in server_events.iter() {
match event {
ServerEvent::ClientConnected(id, user_data) => {
println!("Client {} connected", id);
}
ServerEvent::ClientDisconnected(id) => {
println!("Client {} disconnected", id);
}
}
}
}
```

#### Client
```rust
let mut app = App::new();
app.add_plugin(RenetClientPlugin);

let client = RenetClient::new(...);
app.insert_resource(client);

app.add_system(send_message_system);
app.add_system(receive_message_system);

// Systems

fn send_message_system(mut client: ResMut<RenetClient>) {
let channel_id = 0;
// Send a text message to the server
client.send_message(channel_id, "server message".as_bytes().to_vec());
}

fn receive_message_system(mut client: ResMut<RenetClient>) {
let channel_id = 0;
while let Some(message) = client.receive_message(channel_id) {
// Handle received message
}
}
```

## Example

You can run the `simple` example with:
* Server: `cargo run --example simple -- server`
* Client: `cargo run --example simple -- client`

If you want a more complex example you can checkout the [demo_bevy](https://github.com/lucaspoffo/renet/tree/master/demo_bevy) sample:

[Bevy Demo.webm](https://user-images.githubusercontent.com/35241085/180664609-f8c969e0-d313-45c0-9c04-8a116896d0bd.webm)

0 comments on commit cc8c9db

Please sign in to comment.