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

Method connect

websockets/chat-actorless/src/server.rs:120–139  ·  view source on GitHub ↗

Register new session and assign unique ID to this session

(&mut self, tx: mpsc::UnboundedSender<Msg>)

Source from the content-addressed store, hash-verified

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) {

Callers 5

mainFunction · 0.80
mainFunction · 0.80
runMethod · 0.80
chat_wsFunction · 0.80
init_poolFunction · 0.80

Calls 2

send_system_messageMethod · 0.80
insertMethod · 0.80

Tested by

no test coverage detected