#gamedev #networking

bevy_renet

Bevy plugin for the renet crate: Server/Client network library for multiplayer games with authentication and connection management

6 releases

Uses new Rust 2021

0.0.6 Nov 18, 2022
0.0.5 Aug 1, 2022
0.0.4 Jul 25, 2022
0.0.3 Jun 26, 2022
0.0.1 Apr 30, 2022

#128 in Game dev

Download history 96/week @ 2022-08-18 66/week @ 2022-08-25 51/week @ 2022-09-01 79/week @ 2022-09-08 43/week @ 2022-09-15 93/week @ 2022-09-22 118/week @ 2022-09-29 61/week @ 2022-10-06 75/week @ 2022-10-13 65/week @ 2022-10-20 68/week @ 2022-10-27 99/week @ 2022-11-03 146/week @ 2022-11-10 209/week @ 2022-11-17 92/week @ 2022-11-24 89/week @ 2022-12-01

553 downloads per month
Used in bevy_diagnostic_renet

MIT/Apache

61KB
934 lines

Bevy Renet

Latest version Documentation MIT Apache

A Bevy Plugin for the 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 updateand send_packets. RenetClient and RenetServer need to be added as a resource, so the setup is similar to renet itself:

Server

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

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 sample:

Bevy Demo.webm

Bevy Compatibility

bevy bevy_renet
0.9 0.0.6
0.8 0.0.5
0.7 0.0.4

Dependencies

~12–22MB
~434K SLoC