Skip to main content

Rooms

Struct Rooms 

Source
pub struct Rooms { /* private fields */ }
Expand description

Every socket currently connected, grouped by a key you choose.

The thing every non-trivial socket application needs and none of them want to write twice: a chat needs the room’s members, a live dashboard needs everyone watching one account.

ⓘ
rooms.join("lobby", socket.clone());
rooms.send("lobby", Message::text("someone arrived"));
rooms.leave("lobby", socket.id());

Handles to sockets that have gone away are dropped on the next send rather than reaped on a timer, so a client that vanished without a close frame costs one failed send and then nothing.

Implementations§

Source§

impl Rooms

Source

pub fn new() -> Self

An empty registry.

Source

pub fn join(&self, room: impl Into<String>, socket: Socket)

Add socket to room.

Source

pub fn leave(&self, room: &str, socket: SocketId)

Remove one socket from room.

Source

pub fn leave_all(&self, socket: SocketId)

Remove a socket from every room.

What on_close should call: a handler that tracked which rooms a socket was in would be keeping a second copy of this map.

Source

pub fn send(&self, room: &str, message: impl Into<Message>) -> usize

Send to everyone in room. Returns how many received it.

Source

pub fn send_except( &self, room: &str, except: SocketId, message: impl Into<Message>, ) -> usize

Send to everyone in room except one socket — the one that caused the message, which has usually shown it already.

Source

pub fn count(&self, room: &str) -> usize

How many sockets are in room.

Source

pub fn rooms(&self) -> Vec<String>

The rooms that have anyone in them.

Trait Implementations§

Source§

impl Debug for Rooms

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Rooms

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !Freeze for Rooms

§

impl RefUnwindSafe for Rooms

§

impl Send for Rooms

§

impl Sync for Rooms

§

impl Unpin for Rooms

§

impl UnsafeUnpin for Rooms

§

impl UnwindSafe for Rooms

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.