Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(socketio/operators): get all rooms from io #248

Merged
merged 2 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# 0.10.2
## socketioxide
* New [`rooms`](https://docs.rs/socketioxide/latest/socketioxide/struct.SocketIo.html#method.rooms) fn to get all the rooms of a namespace.

# 0.10.1
## socketioxide
* New [`as_str`](https://docs.rs/socketioxide/latest/socketioxide/socket/struct.Sid.html#method.as_str) fn for `Sid`.
* Http request is now cloned for the websocket transport (it was not possible before http v1). Therefore it is possible to get headers/extensions of the initial request.

# 0.10.0
## socketioxide
* Rework for `emit_with_ack` fns. It now returns an `AckStream` that can be used either as a future when expecting one ack or as a stream when expecting multiple acks. When expecting multiple acks the `AckStream` will yield `AckResult`s as well as their corresponding socket `id`.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace.package]
version = "0.10.1"
version = "0.10.2"
edition = "2021"
rust-version = "1.67.0"
authors = ["Théodore Prévot <"]
Expand Down
2 changes: 1 addition & 1 deletion socketioxide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "../README.md"


[dependencies]
engineioxide = { path = "../engineioxide", version = "0.10.1" }
engineioxide = { path = "../engineioxide", version = "0.10.2" }
futures.workspace = true
tokio = { workspace = true, features = ["rt", "time"] }
serde.workspace = true
Expand Down
7 changes: 7 additions & 0 deletions socketioxide/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ pub trait Adapter: std::fmt::Debug + Send + Sync + 'static {
/// Disconnects the sockets that match the [`BroadcastOptions`].
fn disconnect_socket(&self, opts: BroadcastOptions) -> Result<(), Vec<DisconnectError>>;

/// Returns all the rooms for this adapter.
fn rooms(&self) -> Result<Vec<Room>, Self::Error>;

//TODO: implement
// fn server_side_emit(&self, packet: Packet, opts: BroadcastOptions) -> Result<u64, Error>;
// fn persist_session(&self, sid: i64);
Expand Down Expand Up @@ -278,6 +281,10 @@ impl Adapter for LocalAdapter {
Err(errors)
}
}

fn rooms(&self) -> Result<Vec<Room>, Self::Error> {
Ok(self.rooms.read().unwrap().keys().cloned().collect())
}
}

impl LocalAdapter {
Expand Down
23 changes: 22 additions & 1 deletion socketioxide/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use engineioxide::{

use crate::{
ack::AckStream,
adapter::{Adapter, LocalAdapter},
adapter::{Adapter, LocalAdapter, Room},
client::Client,
extract::SocketRef,
handler::ConnectHandler,
Expand Down Expand Up @@ -734,6 +734,27 @@ impl<A: Adapter> SocketIo<A> {
self.get_default_op().join(rooms)
}

/// Gets all room names on the current namespace.
///
/// Alias for `io.of("/").unwrap().rooms()`
///
/// ## Panics
/// If the **default namespace "/" is not found** this fn will panic!
///
/// ### Example
/// ```
/// # use socketioxide::{SocketIo, extract::SocketRef};
/// let (_, io) = SocketIo::new_svc();
/// let io2 = io.clone();
/// io.ns("/", move |socket: SocketRef| async move {
/// println!("Socket connected on /test namespace with id: {}", socket.id);
/// let rooms = io2.rooms().unwrap();
/// println!("All rooms on / namespace: {:?}", rooms);
/// });
pub fn rooms(&self) -> Result<Vec<Room>, A::Error> {
self.get_default_op().rooms()
}

/// Makes all sockets selected with the previous operators leave the given room(s).
///
/// Alias for `io.of("/").unwrap().join(rooms)`
Expand Down
5 changes: 5 additions & 0 deletions socketioxide/src/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,11 @@ impl<A: Adapter> Operators<A> {
self.ns.adapter.del_sockets(self.opts, rooms)
}

/// Gets all room names for a given namespace
pub fn rooms(self) -> Result<Vec<Room>, A::Error> {
self.ns.adapter.rooms()
}

/// Creates a packet with the given event and data.
fn get_packet(
&mut self,
Expand Down