pub struct Room { /* private fields */ }Expand description
A joined room.
Cheap to clone. The actor behind it runs while any clone exists, and
leave ends it for all of them.
Implementations§
Source§impl Room
impl Room
Sourcepub fn for_each_member<F, Fut>(
&self,
name: &str,
task: F,
) -> AbortOnDropHandle<()> ⓘ
pub fn for_each_member<F, Fut>( &self, name: &str, task: F, ) -> AbortOnDropHandle<()> ⓘ
Runs task for every member that publishes name, as the membership changes.
Starts task(member) once a member lists name, and aborts it once
the member no longer does. A task that ends on its own is started
again only after the member lists name anew, so a task that should
outlast a closed broadcast retries itself, for example with
subscriptions. Runs until the room is left,
its last handle drops, or the returned handle drops. It holds no
handle of the room itself, so it does not keep the room joined.
§Panics
Panics when called outside a Tokio runtime.
§Examples
let _readers = room.for_each_member("cam", |member| async move {
println!("{} publishes a camera", member.fmt_short());
});Sourcepub fn subscriptions(&self, peer: EndpointId, name: &str) -> MemberSubscriptions
pub fn subscriptions(&self, peer: EndpointId, name: &str) -> MemberSubscriptions
Returns the subscriptions to member peer’s broadcast name, a new one after each end while the member lists it.
The first next waits for the member to
list name, however long, so bound the wait if it may never do. Each
later one waits for the previous subscription to close, such as when
the member restarts its broadcast or the session with it fails, and
subscribes again. After a subscription that failed, or closed within
sixteen seconds, it waits first, from two seconds up to sixteen, since
a member that vanished stays listed for minutes.
The subscriptions end once the member stops listing name after it
did, the room is left, or its last handle drops. They hold a handle
of the room only while they subscribe, so they do not keep the room
joined.
§Examples
let mut subscriptions = room.subscriptions(peer, "cam");
while let Some(subscription) = subscriptions.next().await {
let _broadcast = subscription.broadcast();
// Read the broadcast until it closes, then take the next one.
}Sourcepub fn read_values<T>(&self, name: &str, track: &str) -> MemberValues<T>where
T: DeserializeOwned + Send + 'static,
pub fn read_values<T>(&self, name: &str, track: &str) -> MemberValues<T>where
T: DeserializeOwned + Send + 'static,
Reads track track of broadcast name from every member that publishes it.
The counterpart of publish_value: each member
reports its newest value, and None once it no longer lists name.
A member is read through subscriptions, so
one whose subscription ends while it still lists name, such as one
whose session failed, is read again. A value that does not decode is
skipped, and a member whose broadcast has no track track is not
read until it lists name anew. Dropping the returned
MemberValues stops every reader. The readers do not keep the room
joined.
§Panics
Panics when called outside a Tokio runtime.
§Examples
let mut presence = room.read_values::<String>("presence", "status");
while let Some(update) = presence.recv().await {
let member = update.member.fmt_short();
match update.value {
Some(status) => println!("{member}: {}", status.value),
None => println!("{member} left"),
}
}Source§impl Room
impl Room
Sourcepub fn ticket(&self) -> RoomTicket
pub fn ticket(&self) -> RoomTicket
Returns a ticket for this room with this member as its bootstrap peer.
Sourcepub fn state(&self) -> Direct<RoomState>
pub fn state(&self) -> Direct<RoomState>
Returns the members, their broadcasts and connections, as they change.
Sourcepub fn publish<B>(&self, name: &str) -> Result<B, Error>
pub fn publish<B>(&self, name: &str) -> Result<B, Error>
Publishes a broadcast into the room as name, to its members only, and returns it.
The path is rooms/<this member>/<topic>/<name>. B is built from the
route table’s broadcast::Producer: a LocalBroadcast, or the producer
itself. The broadcast stays announced until it closes,
unpublish takes it out, or the room is left.
§Errors
Fails with Error::InvalidName for an empty name or one holding a
slash, Error::Node if the name is already published or B
cannot be built, and Error::Left once the room was left.
§Panics
Panics when called outside a Tokio runtime.
Sourcepub fn publish_value<T: Serialize>(
&self,
name: &str,
track: &str,
) -> Result<ValueWriter<T>, Error>
pub fn publish_value<T: Serialize>( &self, name: &str, track: &str, ) -> Result<ValueWriter<T>, Error>
Publishes a broadcast into the room as name whose one track track holds a latest-value JSON value.
For state the members share, such as presence, a cursor or a game:
each update replaces the value, and a member
that subscribes late gets the current one at once. Members read it
from everyone with read_values, or from one
member with subscribe and
[Subscription::value_track]. The track exists before the broadcast
is announced, so no member misses it, and the broadcast carries no
media catalog. The last clone of the writer to drop finishes the track
and closes the broadcast, which takes name out of the room. Otherwise
it stays as publish describes.
§Errors
As publish.
§Panics
Panics when called outside a Tokio runtime.
§Examples
let presence = room.publish_value::<String>("presence", "status")?;
presence.update(&"typing".to_owned())?;Sourcepub fn unpublish(&self, name: &str) -> bool
pub fn unpublish(&self, name: &str) -> bool
Takes name out of the room, if this member publishes it.
The broadcast stays open for whoever holds it, unannounced. Returns
whether name was published.
Sourcepub async fn subscribe(
&self,
peer: EndpointId,
name: &str,
) -> Result<Subscription, Error>
pub async fn subscribe( &self, peer: EndpointId, name: &str, ) -> Result<Subscription, Error>
Resolves peer’s broadcast name over the session with that member.
Reads what the member itself announces on that session, so no other
peer can stand in for it. Waits until the member announces name, so
bound the wait if the member may never publish it.
§Errors
Fails with Error::Left once the room was left, and with
Error::Node if the member cannot be dialed, the node’s policy does
not grant the member its RoomsLayout scope, or the session ends
before the member announces name.
Sourcepub fn set_display_name(&self, name: Option<String>)
pub fn set_display_name(&self, name: Option<String>)
Sets the name other members see, or clears it.
Sourcepub async fn leave(&self)
pub async fn leave(&self)
Leaves the room for every clone of this handle.
Tells the other members and withdraws this member’s publications.
Subscriptions made with subscribe stay open, so
drop them too. Returns within seconds even if every other member is
gone. Idempotent. Not cancellation safe: call it again to finish.