bevy_client_server_events 0.4.1

Simplified game networking
Documentation

Bevy Client Server Events

Bevy Client Server Events Latest version Documentation MIT Apache

Simple event-based client-server networking library for Bevy.

Easily send bevy events to/from a client/server without worrying about serialization or network transport details.

Builds off of the renet/bevy_renet library and attempts to simplify the configuration and management of types to be sent through a network.

Goals:

  • Simplified network setup and configuration
  • Easily send any types to/from a client/server

Todo:

  • Support secure authentication

Examples

See the examples/ping.rs file for a simple ping-pong example.

In one terminal session, start the server: cargo run --example ping -- -s

In another terminal session, connect with a client: cargo run --example ping

With the client window in focus, hit ENTER to send a Ping. The server will respond with a Pong.

Overview

In this example, we want to send a Ping event to the server and receive a Pong event in return.

#[derive(Event, Encode, Decode)]
pub struct Ping;

#[derive(Event, Encode, Decode)]
pub struct Pong;

As of version 0.4, the Event, Encode, and Decode derives are required.

When setting up our App, we need to feed it to a macro and provide all the events to be sent over the network.

fn main() {
    // ...
    let mut app = App::new();
    client_server_events_plugin!(
        app,
        Ping => NetworkConfig::default(),
        Pong => NetworkConfig::default()
    );
    // ...

You can provide type-specific network configuration, such as reliability, resend time, max memory usage, etc.

The macro should be run regardless of whether this instance will be a server or a client.

You can choose to start a server instance or connect to a server as a client using events.

fn start_server(mut start_server: EventWriter<StartServer>) {
    start_server.send(StartServer::default()); // Binds to 127.0.0.1:5000 by default.
}

fn connect_as_client(mut connect_to_server: EventWriter<ConnectToServer>) {
    connect_to_server.send(ConnectToServer::default()); // Connects to 127.0.0.1:5000 by default.
}

Then you can send/receive events as desired.

fn update_client(
    mut send_ping: EventWriter<SendToServer<Ping>>,
    mut receive_pong: EventReader<ReceiveFromServer<Pong>>,
) {
    // ...
    send_ping.send(SendToServer { content: Ping });
    // ...
    for ReceiveFromServer { content } in receive_pong.iter() {
        // Do something with content (Pong).
        // ...
    }
}

fn update_server(
    mut receive_ping: EventReader<ReceiveFromClient<Ping>>,
    mut send_pong: EventWriter<SendToClient<Pong>>,
) {
    for ReceiveFromClient { client_id, content } in receive_ping.iter() {
        // Do something with content (Ping).
        send_pong.send(SendToClient {
            client_id,
            content: Pong,
        });
    }
}

You can also broadcast a message to all clients using EventWriter<SendToClients<T>>.

Networking errors can be handled via EventReader<NetcodeTransportError>.

fn handle_errors(mut errors: EventReader<NetcodeTransportError>) {
    for error in errors.iter() {
        println!("Networking Error: {:?}", error);
    }
}

Bevy Compatibility

bevy bevy_client_server_events
0.11 0.4.1