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

Function process_text_msg

websockets/chat-actorless/src/handler.rs:89–155  ·  view source on GitHub ↗
(
    chat_server: &ChatServerHandle,
    session: &mut actix_ws::Session,
    text: &str,
    conn: ConnId,
    name: &mut Option<String>,
)

Source from the content-addressed store, hash-verified

87}
88
89async fn process_text_msg(
90 chat_server: &ChatServerHandle,
91 session: &mut actix_ws::Session,
92 text: &str,
93 conn: ConnId,
94 name: &mut Option<String>,
95) {
96 // strip leading and trailing whitespace (spaces, newlines, etc.)
97 let msg = text.trim();
98
99 // we check for /<cmd> type of messages
100 if msg.starts_with('/') {
101 let mut cmd_args = msg.splitn(2, ' ');
102
103 // unwrap: we have guaranteed non-zero string length already
104 match cmd_args.next().unwrap() {
105 "/list" => {
106 log::info!("conn {conn}: listing rooms");
107
108 let rooms = chat_server.list_rooms().await;
109
110 for room in rooms {
111 session.text(room).await.unwrap();
112 }
113 }
114
115 "/join" => match cmd_args.next() {
116 Some(room) => {
117 log::info!("conn {conn}: joining room {room}");
118
119 chat_server.join_room(conn, room).await;
120
121 session.text(format!("joined {room}")).await.unwrap();
122 }
123
124 None => {
125 session.text("!!! room name is required").await.unwrap();
126 }
127 },
128
129 "/name" => match cmd_args.next() {
130 Some(new_name) => {
131 log::info!("conn {conn}: setting name to: {new_name}");
132 name.replace(new_name.to_owned());
133 }
134 None => {
135 session.text("!!! name is required").await.unwrap();
136 }
137 },
138
139 _ => {
140 session
141 .text(format!("!!! unknown command: {msg}"))
142 .await
143 .unwrap();
144 }
145 }
146 } else {

Callers

nothing calls this directly

Calls 3

list_roomsMethod · 0.45
join_roomMethod · 0.45
send_messageMethod · 0.45

Tested by

no test coverage detected