#server #bevy #lan #browser #networking #game-engine #gamedev

bevy_server_browser

Bevy game engine plugin for creating and searching discoverable servers on local networks

5 releases (breaking)

0.5.0 Mar 11, 2024
0.4.0 Jan 3, 2024
0.3.0 Jan 2, 2024
0.2.0 Dec 25, 2023
0.1.0 Dec 25, 2023

#593 in Game dev

Download history 31/week @ 2023-12-24 28/week @ 2023-12-31 4/week @ 2024-02-18 166/week @ 2024-03-10 5/week @ 2024-03-17 41/week @ 2024-03-31

212 downloads per month

MIT/Apache

31KB
291 lines

bevy_server_browser

crates.io docs.rs

Bevy game engine plugin for creating and searching discoverable servers on local networks.

This plugin does not provide any connection between server and clients, you need to pair it with network library, for example bevy_matchbox. This plugin only allows clients to discover servers and its info on local network, so you do not have to type ip addresses of servers into clients.

MSRV: Minimum Supported Rust Version is rust 1.75

Usage

See usage below or examples for more comprehensive usage.

This example shows both server and client in one single app, meaning the client will discover itself, you can use both functionalities or just client or server.

use bevy::prelude::*;
use bevy_server_browser::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        // Add the server browser plugin
        .add_plugins(ServerBrowserPlugin::new("test_id"))
        .add_systems(
            Startup,
            // run discover servers after setup
            (setup_discoverable_server, discover_servers).chain(),
        )
        .add_systems(
            Update,
            print_discovered_servers.run_if(resource_changed::<DiscoveredServerList>()),
        )
        .run();
}

fn setup_discoverable_server(mut commands: Commands) {
    // add discoverable server as a resource which makes it available for discovery
    // on local network

    info!("Adding discoverable server");
    commands.insert_resource(DiscoverableServer {
        port: 1234,
        metadata: ServerMetadata::new().with("name", "Test Server"),
    });
}

fn discover_servers(mut search_servers: EventWriter<SearchServers>) {
    // send SearchServers event which will trigger search of discoverable servers
    // and update Res<DiscoverableServerList> accordingly
    search_servers.send_default();
}

fn print_discovered_servers(servers: Res<DiscoveredServerList>) {
    if servers.is_empty() {
        info!("No servers discovered");
        return;
    }

    info!("Discovered {} servers:", servers.len());
    for server in &servers {
        info!(
            "Name '{}' ({}) with addresses {:?} on port {}",
            server.metadata.get("name").unwrap_or("Unknown Name"),
            server.hostname, server.addresses, server.port
        );
    }
}

bevy bevy_server_browser
0.13 0.5.0
0.12 0.1.0 - 0.4.0

Dependencies

~8–20MB
~242K SLoC