housleyjk / ws-rs

Lightweight, event-driven WebSockets for Rust.
MIT License
1.47k stars 220 forks source link

send message to specific socket #232

Open perlish opened 6 years ago

perlish commented 6 years ago

how it's possible to send message to specific connection_id or token? similar to broadcast but array of users, actually i need to send multicast message

mverleg commented 5 years ago

Maybe you could do something like this? Not sure if it's the best way...

To send a message to a connection, you'd use the Sender object.

So if you want to send by some kind of token, you need to associate those to Senders, e.g.

let mut user_connections: RwLock<HashMap<UserId, Rc<ws::Sender>>> = RwLock::new(HashMap::new());

Where I've added the Mutex because this structure is writable by multiple connections. Maybe your architecture has a way around it, or you have a better datastructure.

Then sending is just something like:

user_connections.get_mut().unwrap().get(&UserId::new(7)).unwrap().send("my data");
perlish commented 5 years ago

thanks, but i offered a new method https://github.com/housleyjk/ws-rs/pull/233