pub async fn perform_client_handshake(stream: TcpStream) -> ResultExamples found in repository?
examples/continue_client.rs (line 7)
6async fn handle_connection(stream: TcpStream) {
7 match perform_client_handshake(stream).await {
8 Ok(mut ws_connection) => {
9 let my_random_string = generate_random_string();
10 println!("Sending random string: {}", my_random_string);
11 if ws_connection
12 .send_large_data_fragmented(Vec::from(my_random_string))
13 .await
14 .is_err()
15 {
16 eprintln!("Error occurred when sending data in chunks");
17 }
18
19 ws_connection.close_connection().await.unwrap();
20 }
21 Err(err) => eprintln!("Error when performing handshake: {}", err),
22 }
23}More examples
examples/client.rs (line 9)
8async fn handle_connection(stream: TcpStream) {
9 match perform_client_handshake(stream).await {
10 Ok(mut ws_connection) => {
11 let mut ticker = interval(Duration::from_secs(5));
12 // it will be used for closing the connection
13 let mut counter = 0;
14
15 loop {
16 select! {
17 Some(result) = ws_connection.read.recv() => {
18 match result {
19 Ok(message) => {
20 println!("Received message: {}", &String::from_utf8(message).unwrap());
21 counter = counter + 1;
22 // close the connection if 3 messages have already been sent and received
23 if counter >= 3 {
24 if ws_connection.close_connection().await.is_err() {
25 eprintln!("Error occurred when closing connection");
26 }
27 break;
28 }
29 }
30 Err(err) => {
31 eprintln!("Received error from the stream: {}", err);
32 break;
33 }
34 }
35 }
36 _ = ticker.tick() => {
37 let random_string = generate_random_string();
38 let binary_data = Vec::from(random_string);
39
40 if ws_connection.send_data(binary_data).await.is_err() {
41 eprintln!("Failed to send message");
42 break;
43 }
44 }
45 }
46 }
47 }
48 Err(err) => eprintln!("Error when performing handshake: {}", err),
49 }
50}