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

Function main

websockets/echo-actorless/src/client.rs:12–74  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10
11#[actix_web::main]
12async fn main() {
13 env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
14
15 log::info!("starting echo WebSocket client");
16
17 let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
18 let mut cmd_rx = UnboundedReceiverStream::new(cmd_rx);
19
20 // run blocking terminal input reader on separate thread
21 let input_thread = thread::spawn(move || {
22 loop {
23 let mut cmd = String::with_capacity(32);
24
25 if io::stdin().read_line(&mut cmd).is_err() {
26 log::error!("error reading line");
27 return;
28 }
29
30 cmd_tx.send(cmd).unwrap();
31 }
32 });
33
34 let (res, mut ws) = awc::Client::new()
35 .ws("ws://127.0.0.1:8080/ws")
36 .connect()
37 .await
38 .unwrap();
39
40 log::debug!("response: {res:?}");
41 log::info!("connected; server will echo messages sent");
42
43 loop {
44 select! {
45 Some(msg) = ws.next() => {
46 match msg {
47 Ok(ws::Frame::Text(txt)) => {
48 // log echoed messages from server
49 log::info!("Server: {txt:?}")
50 }
51
52 Ok(ws::Frame::Ping(_)) => {
53 // respond to ping probes
54 ws.send(ws::Message::Pong(Bytes::new())).await.unwrap();
55 }
56
57 _ => {}
58 }
59 }
60
61 Some(cmd) = cmd_rx.next() => {
62 if cmd.is_empty() {
63 continue;
64 }
65
66 ws.send(ws::Message::Text(cmd.into())).await.unwrap();
67 }
68
69 else => break

Callers

nothing calls this directly

Calls 1

connectMethod · 0.80

Tested by

no test coverage detected