Skip to content

Instantly share code, notes, and snippets.

@Fedcomp
Created June 27, 2020 05:23
Show Gist options
  • Save Fedcomp/2dec478bca59a0700e97a23e92be75b9 to your computer and use it in GitHub Desktop.
Save Fedcomp/2dec478bca59a0700e97a23e92be75b9 to your computer and use it in GitHub Desktop.
tokio-tungstenite simple https example
[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"
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