MudEngine Part 11: Peer-to-Peer with iroh
Add the join handshake
Until now ClientMessage had only Move, Say, Take, and Drop — the
WebSocket server learned a player's identity from the connection itself. On a P2P
mesh there is no connection per player; everyone shares one gossip topic. The
host needs to be introduced: a new player broadcasts Join { name } carrying
its own player_id, and whoever resolves to be the host registers them and
answers with a targeted State snapshot.
The full messages.rs — the join variant is the only addition:
use serde::{Deserialize, Serialize}; pub use super::persistence::{PlayerInfo, RoomInfo}; // ── Shared message types ── /// Quest info sent to the client in the State message — the client renders /// its quest UI from this data, nothing is hardcoded. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct QuestInfo { pub name: String, pub active: bool, pub completed: bool, pub guardians: Vec<QuestGuardianInfo>, } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct QuestGuardianInfo { pub name: String, pub room_id: usize, pub label: String, pub active: bool, } #[derive(Serialize, Deserialize, Debug, Clone)] pub enum ClientMessage { /// The join handshake: a new player introduces itself to the host. /// Whoever resolves to be the host registers the player and replies /// with a targeted `ServerMessage::State`. Join { name: String }, Move { direction: String }, Say { message: String }, Take { item: String }, Drop { item: String }, } #[derive(Serialize, Deserialize, Debug, Clone)] pub enum ServerMessage { State { players: Vec<PlayerInfo>, your_id: String, rooms: Vec<RoomInfo>, /// Quest data so the client can render quest status immediately /// — None when Game.toml has no `[quest]` section. quest: Option<QuestInfo>, }, PlayerJoined(PlayerInfo), PlayerMoved(PlayerInfo), PlayerLeft { id: String, }, Chat { from: String, message: String, room: usize, }, ItemTaken { player_id: String, player_name: String, item: String, }, ItemDropped { player_id: String, player_name: String, item: String, room: usize, }, QuestEvent { active: bool, message: String, }, QuestCompleted { message: String, }, }
Every ServerMessage is broadcast to the whole mesh. State carries a
your_id so each client applies the snapshot only when the field matches its
own id — otherwise every player would constantly overwrite its world with the
last joiner's snapshot. player_id (a fresh uuid per session) is the client-side
identity; name is what appears in the roster.