-
Hello there! First of all thanks for this amazing project, its making my life super easy :D I have the following use case: In my server I differentiate between 2 type of clients: "Controllers" and "Receivers". Since the messages that controllers and receivers can send is completely different, I created 2 namespaces "/controller" and "/receiver" The clients will choose what role they want to be when connecting to the server. The thing is that Controllers actually need to send messages to receivers in a specific scenario, but since they are in a different namespace I don't know how to do it. If it was possible to get a reference to the global I am starting to think that I am actually not implementing my server correctly and that I should think of another way of namespacing the operations that each role can perform Any help is appreciated :D |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
As a workaround (Which could be actually just the proper solution) I've done the following: #[derive(Deserialize, Clone, Copy)]
enum Role {
Receiver,
Controller,
}
pub fn on_connect(socket: SocketRef, Data(role): Data<Role>) {
info!("Socket.IO connected: {:?} {:?}", socket.ns(), socket.id);
match role {
Role::Receiver => receiver::on_connect(&socket),
Role::Controller => controller::on_connect(&socket),
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing::subscriber::set_global_default(FmtSubscriber::default())?;
let sessions = Sessions::new();
let (layer, io) = SocketIoBuilder::new().with_state(sessions).build_layer();
io.ns("/", on_connect);
let app = axum::Router::new().layer(layer);
info!("Starting server");
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
Ok(())
} I haven't tested it yet but I assume I will be able to pass the role when connecting :D |
Beta Was this translation helpful? Give feedback.
-
You could just clone an IO ref in your desired scope and use |
Beta Was this translation helpful? Give feedback.
You could just clone an IO ref in your desired scope and use
io.of("/consumer").{get_socket,emit,in,...}
from your receiver ?https://docs.rs/socketioxide/latest/socketioxide/struct.SocketIo.html#method.of