pub struct Room<S> { /* private fields */ }Expand description
A set of peers sharing presence/state — one game world, one lobby, one chat
channel. S is app-defined per-peer state (position, name, flags, …); Resuma
only stores and returns it.
Extracted from a real multiplayer game’s hand-rolled HashMap<String, PeerLive>
- broadcast/sweep helpers so future apps do not re-derive the same ~150 lines.
Implementations§
Source§impl<S> Room<S>
impl<S> Room<S>
pub fn new(max_peers: usize) -> Self
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn get(&self, id: &str) -> Option<&Peer<S>>
pub fn get_mut(&mut self, id: &str) -> Option<&mut Peer<S>>
Sourcepub fn peers_except<'a>(
&'a self,
except: &'a str,
) -> impl Iterator<Item = &'a Peer<S>>
pub fn peers_except<'a>( &'a self, except: &'a str, ) -> impl Iterator<Item = &'a Peer<S>>
Every peer except except (pass "" to include everyone).
Sourcepub fn join(
&mut self,
id: PeerId,
state: S,
tx: UnboundedSender<String>,
) -> Result<Option<Peer<S>>, RoomFull>
pub fn join( &mut self, id: PeerId, state: S, tx: UnboundedSender<String>, ) -> Result<Option<Peer<S>>, RoomFull>
Insert a new peer. If id was already connected (e.g. a browser tab
refresh reusing a client-generated id), the stale entry is replaced and
returned so the caller can broadcast a “left” event for it before
announcing the new join.
Errors with RoomFull once max_peers is reached and id is new.
pub fn leave(&mut self, id: &str) -> Option<Peer<S>>
Sourcepub fn broadcast_except(&self, except: &str, msg: &str)
pub fn broadcast_except(&self, except: &str, msg: &str)
Send msg to every peer except except (pass "" to include everyone).
pub fn broadcast(&self, msg: &str)
Sourcepub fn sweep_stale(&mut self, timeout: Duration) -> Vec<PeerId> ⓘ
pub fn sweep_stale(&mut self, timeout: Duration) -> Vec<PeerId> ⓘ
Remove peers whose Peer::last_seen is older than timeout and return
their ids so callers can broadcast a “left” event / clean up app-side state.
Browser tabs get heavily throttled in the background (timers can pause for
tens of seconds while the socket stays open), so timeout should be
generous — minutes, not seconds — or reconnecting tabs look like churn.