bevy_server_browser

Crates.iobevy_server_browser
lib.rsbevy_server_browser
version0.5.0
sourcesrc
created_at2023-12-25 15:05:23.745847
updated_at2024-03-11 16:10:44.332388
descriptionBevy game engine plugin for creating and searching discoverable servers on local networks
homepagehttps://github.com/richardhozak/bevy_server_browser
repositoryhttps://github.com/richardhozak/bevy_server_browser
max_upload_size
id1080291
size121,724
Richard Hozák (richardhozak)

documentation

README

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
Commit count: 7

cargo fmt