Skip to content

richardhozak/bevy_server_browser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages