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

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