MCPcopy Create free account
hub / github.com/actix/examples / handle

Method handle

websockets/chat/src/server.rs:119–137  ·  view source on GitHub ↗
(&mut self, msg: Connect, _: &mut Context<Self>)

Source from the content-addressed store, hash-verified

117 let id = rand::rng().random::<u64>();
118 state.sessions.insert(id, addr);
119
120 // auto join session to main room
121 state.rooms.entry("main".to_owned()).or_default().insert(id);
122
123 let count = state.visitor_count.fetch_add(1, Ordering::SeqCst);
124 state.send_message("main", &format!("Total visitors {count}"), 0);
125
126 // send id back
127 let _ = reply.send(id);
128 }
129 Command::Disconnect { id } => {
130 tracing::info!("Someone disconnected");
131
132 let mut rooms: Vec<String> = Vec::new();
133
134 // remove address
135 if state.sessions.remove(&id).is_some() {
136 // remove session from all rooms
137 for (name, sessions) in &mut state.rooms {
138 if sessions.remove(&id) {
139 rooms.push(name.to_owned());
140 }

Callers

nothing calls this directly

Calls 3

insertMethod · 0.80
as_strMethod · 0.80
send_messageMethod · 0.45

Tested by

no test coverage detected