← All Workshops

MudEngine Part 11: Peer-to-Peer with iroh

Step 9 / 14

Launch from main.rs

The old main.rs had three branches (server / web / native client) and pointed the native client at http://localhost:8080 with set_server_url. There is no URL to point at anymore — the transport is the mesh. main.rs now has two branches:

  • wasm (dx build --web): plain dioxus::launch(App) — serves the same UI, with the relay stub transport behind it (step 2)
  • native (cargo run): dioxus_native::launch_cfg with the window config from Part 10 — this is the process that either hosts the world or dials a host

Everything else — the module header mod components; mod game; — is unchanged from Part 10.

mud-engine/src/main.rs
mod components;
mod game;

use crate::components::app::App;

fn main() {
    #[cfg(target_arch = "wasm32")]
    dioxus::launch(App);

    // Desktop (cargo run): each player runs their own process — one hosts,
    // the rest dial it over iroh's QUIC mesh. No `dx serve`, no HTTP server.
    #[cfg(not(target_arch = "wasm32"))]
    {
        use dioxus_native::{Config, LogicalSize, WindowAttributes};
        let cfg = Config::new().with_window_attributes(
            WindowAttributes::default()
                .with_title("MudEngine")
                .with_inner_size(LogicalSize::new(1024.0, 768.0)),
        );
        dioxus_native::launch_cfg(App, vec![], vec![Box::new(cfg)]);
    }
}
🔍 Why no feature flags in main

Previously #[cfg(feature = "server")] picked the binary's personality. Now the target does: on wasm the code is a browser client; natively it is a desktop mesh node. The role (Host vs Client) is a runtime choice a player makes on the connection screen — not a compile-time one. Both branches launch the same App.

Step 9 / 14