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 update function from the client/server. RenetClient, RenetServer, NetcodeClientTransport and NetcodeServerTransport 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(ConnectionConfig::default()); app.insert_resource(server);

// Transport layer setup app.addplugin(NetcodeServerPlugin); let serveraddr = "127.0.0.1:5000".parse().unwrap(); let socket = UdpSocket::bind(serveraddr).unwrap(); const MAXCLIENTS: usize = 64; const GAMEPROTOCOLID: u64 = 0; let serverconfig = ServerConfig::new(MAXCLIENTS, PROTOCOLID, serveraddr, ServerAuthentication::Unsecure); let currenttime = SystemTime::now().durationsince(SystemTime::UNIXEPOCH).unwrap(); let transport = NetcodeServerTransport::new(currenttime, serverconfig, socket).unwrap(); app.insertresource(transport);

app.addsystem(sendmessagesystem); app.addsystem(receivemessagesystem); app.addsystem(handleevents_system);

// Systems

fn sendmessagesystem(mut server: ResMut) { let channelid = 0; // Send a text message for all clients // The enum DefaultChannel describe the channels used by the default configuration server.broadcastmessage(DefaultChannel::ReliableOrdered, "server message".asbytes().tovec()); }

fn receivemessagesystem(mut server: ResMut) { // Send a text message for all clients for clientid in server.clientsid().intoiter() { while let Some(message) = server.receivemessage(client_id, DefaultChannel::ReliableOrdered) { // Handle received message } } }

fn handleeventssystem(mut serverevents: EventReader) { while let Some(event) = server.getevent() { for event in serverevents.iter() { match event { ServerEvent::ClientConnected { clientid } => { println!("Client {clientid} connected"); } ServerEvent::ClientDisconnected { clientid, reason } => { println!("Client {client_id} disconnected: {reason}"); } } } } ```

Client

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

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

// Setup the transport layer app.addplugin(NetcodeClientPlugin); let publicaddr = "127.0.0.1:5000".parse().unwrap(); let socket = UdpSocket::bind(publicaddr).unwrap(); let serverconfig = ServerConfig { maxclients: 64, protocolid: PROTOCOLID, publicaddr, authentication: ServerAuthentication::Unsecure, }; let currenttime = SystemTime::now().durationsince(SystemTime::UNIXEPOCH).unwrap(); let transport = NetcodeServerTransport::new(currenttime, serverconfig, socket).unwrap(); app.insertresource(transport);

app.addsystem(sendmessagesystem); app.addsystem(receivemessagesystem);

// Systems

fn sendmessagesystem(mut client: ResMut) { // Send a text message to the server client.sendmessage(DefaultChannel::ReliableOrdered, "server message".asbytes().to_vec()); }

fn receivemessagesystem(mut client: ResMut) { while let Some(message) = client.receive_message(DefaultChannel::ReliableOrdered) { // 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.10|0.0.8| |0.9|0.0.6| |0.8|0.0.5| |0.7|0.0.4|