File: tokio-echo.rs

package info (click to toggle)
rust-async-tungstenite 0.28.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 420 kB
  • sloc: makefile: 2
file content (35 lines) | stat: -rw-r--r-- 924 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use async_tungstenite::{tokio::connect_async, tungstenite::Message};
use futures::prelude::*;

async fn run() -> Result<(), Box<dyn std::error::Error>> {
    #[cfg(any(
        feature = "async-tls",
        feature = "tokio-native-tls",
        feature = "tokio-openssl"
    ))]
    let url = "wss://echo.websocket.org";
    #[cfg(not(any(
        feature = "async-tls",
        feature = "tokio-native-tls",
        feature = "tokio-openssl"
    )))]
    let url = "ws://echo.websocket.org";

    let (mut ws_stream, _) = connect_async(url).await?;

    let text = "Hello, World!";

    println!("Sending: \"{}\"", text);
    ws_stream.send(Message::text(text)).await?;

    let msg = ws_stream.next().await.ok_or("didn't receive anything")??;

    println!("Received: {:?}", msg);

    Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let rt = tokio::runtime::Runtime::new()?;
    rt.block_on(run())
}