Created
June 27, 2020 05:23
-
-
Save Fedcomp/2dec478bca59a0700e97a23e92be75b9 to your computer and use it in GitHub Desktop.
tokio-tungstenite simple https example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "tungstenite-example" | |
version = "0.0.1" | |
authors = ["Fedcomp"] | |
edition = "2018" | |
[dependencies] | |
tokio-tungstenite = { version = "0.10.1", features = ["tls"] } | |
tokio = { version = "0.2.21", features = ["full"] } | |
futures = "0.3.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use tokio_tungstenite::connect_async; | |
use futures::{SinkExt, StreamExt}; | |
const URL: &str = "wss://echo.websocket.org"; | |
#[tokio::main] | |
async fn main() { | |
let (mut connection, _) = connect_async(URL).await.expect("Failed to connect"); | |
connection.send("hello".into()).await.expect("Failed to send"); | |
let connect_async_text_response = connection.next().await.expect("No response received").expect("Protocol error"); | |
dbg!(connect_async_text_response); | |
let (mut connection, _) = connect_async(URL).await.expect("Failed to connect"); | |
connection.send((&b"hello"[..]).into()).await.expect("Failed to send"); | |
let connect_async_binary_response = connection.next().await.expect("No response received").expect("Protocol error"); | |
dbg!(connect_async_binary_response); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment