Register new session and assign unique ID to this session
(&mut self, tx: mpsc::UnboundedSender<Msg>)
| 118 | |
| 119 | /// Register new session and assign unique ID to this session |
| 120 | async fn connect(&mut self, tx: mpsc::UnboundedSender<Msg>) -> ConnId { |
| 121 | log::info!("Someone joined"); |
| 122 | |
| 123 | // notify all users in same room |
| 124 | self.send_system_message("main", 0, "Someone joined").await; |
| 125 | |
| 126 | // register session with random connection ID |
| 127 | let id = rand::rng().random::<ConnId>(); |
| 128 | self.sessions.insert(id, tx); |
| 129 | |
| 130 | // auto join session to main room |
| 131 | self.rooms.entry("main".to_owned()).or_default().insert(id); |
| 132 | |
| 133 | let count = self.visitor_count.fetch_add(1, Ordering::SeqCst); |
| 134 | self.send_system_message("main", 0, format!("Total visitors {count}")) |
| 135 | .await; |
| 136 | |
| 137 | // send id back |
| 138 | id |
| 139 | } |
| 140 | |
| 141 | /// Unregister connection from room map and broadcast disconnection message. |
| 142 | async fn disconnect(&mut self, conn_id: ConnId) { |