MCPcopy Create free account
hub / github.com/bedroombuilds/python2rust / user_connected

Function user_connected

22_websockets_server/rust/warp_ws/src/main.rs:35–66  ·  view source on GitHub ↗
(ws: WebSocket, users: Users)

Source from the content-addressed store, hash-verified

33}
34
35async fn user_connected(ws: WebSocket, users: Users) {
36 let my_id = NEXT_USER_ID.fetch_add(1, Ordering::Relaxed);
37
38 eprintln!("new chat user: {}", my_id);
39
40 let (user_ws_tx, mut user_ws_rx) = ws.split();
41
42 let (tx, rx) = mpsc::unbounded_channel();
43 let rx = UnboundedReceiverStream::new(rx);
44 tokio::task::spawn(rx.forward(user_ws_tx).map(|result| {
45 if let Err(e) = result {
46 eprintln!("websocket send error: {}", e);
47 }
48 }));
49
50 users.write().await.insert(my_id, tx);
51
52 let users2 = users.clone();
53
54 while let Some(result) = user_ws_rx.next().await {
55 let msg = match result {
56 Ok(msg) => msg,
57 Err(e) => {
58 eprintln!("websocket error(uid={}): {}", my_id, e);
59 break;
60 }
61 };
62 user_message(my_id, msg, &users).await;
63 }
64
65 user_disconnected(my_id, &users2).await;
66}
67
68async fn user_message(my_id: usize, msg: Message, users: &Users) {
69 // Skip any non-Text messages...

Callers 1

mainFunction · 0.85

Calls 3

user_messageFunction · 0.85
user_disconnectedFunction · 0.85
nextMethod · 0.45

Tested by

no test coverage detected