Function socket_flow::handshake::connect_async

source ยท
pub async fn connect_async(addr: &str) -> Result
Expand description

Used for connecting as a client to a websocket endpoint.

It basically does the first step of genereating the client key going to the second step, which is parsing the server reponse, finally creating the connection, and returning a WSConnection

Examples found in repository?
examples/autobahn_client.rs (line 11)
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
36
37
38
39
40
41
42
43
async fn run_test(case: u32) -> Result<(), Error> {
    info!("Running test case {}", case);
    let case_url = &format!("ws://127.0.0.1:9001/runCase?case={}&agent={}", case, AGENT);
    let mut connection = connect_async(case_url).await?;
    while let Some(msg) = connection.next().await {
        let msg = msg?;
        connection.send_message(msg).await?;
    }

    Ok(())
}

async fn update_reports() -> Result<(), Error> {
    info!("updating reports");
    let mut connection = connect_async(&format!(
        "ws://127.0.0.1:9001/updateReports?agent={}",
        AGENT
    ))
    .await?;
    info!("closing connection");
    connection.close_connection().await?;
    Ok(())
}

async fn get_case_count() -> Result<u32, Error> {
    let mut connection = connect_async("ws://localhost:9001/getCaseCount").await?;

    // Receive a single message
    let msg = connection.next().await.unwrap()?;
    connection.close_connection().await?;

    let text_message = msg.as_text()?;
    Ok(text_message
        .parse::<u32>()
        .expect("couldn't convert test case to number"))
}
More examples
Hide additional examples
examples/continue_client.rs (line 7)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
async fn handle_connection(addr: &str) {
    match connect_async(addr).await {
        Ok(mut ws_connection) => {
            let my_random_string = generate_random_string();
            info!("Sending random string: {}", my_random_string);
            if ws_connection
                .send_large_data_fragmented(Vec::from(my_random_string))
                .await
                .is_err()
            {
                error!("Error occurred when sending data in chunks");
            }

            ws_connection.close_connection().await.unwrap();
        }
        Err(err) => error!("Error when performing handshake: {}", err),
    }
}
examples/client.rs (line 10)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
async fn handle_connection(addr: &str) {
    match connect_async(addr).await {
        Ok(mut ws_connection) => {
            let mut ticker = interval(Duration::from_secs(5));
            // it will be used for closing the connection
            let mut counter = 0;

            loop {
                select! {
                    Some(result) = ws_connection.next() => {
                        match result {
                            Ok(message) => {
                                 info!("Received message: {}", message.as_text().unwrap());
                                counter = counter + 1;
                                // close the connection if 3 messages have already been sent and received
                                if counter >= 3 {
                                    if ws_connection.close_connection().await.is_err() {
                                         error!("Error occurred when closing connection");
                                    }
                                    break;
                                }
                            }
                            Err(err) => {
                                error!("Received error from the stream: {}", err);

                                break;
                            }
                        }
                    }
                    _ = ticker.tick() => {
                        let random_string = generate_random_string();
                        let binary_data = Vec::from(random_string);

                        if ws_connection.send(binary_data).await.is_err() {
                            eprintln!("Failed to send message");
                            break;
                        }
                    }
                }
            }
        }
        Err(err) => error!("Error when performing handshake: {}", err),
    }
}