Skip to content

Instantly share code, notes, and snippets.

@FreeFull
Last active November 16, 2019 13:07
Show Gist options
  • Select an option

  • Save FreeFull/936f68d4d7410b5b89c1e4552fb6c318 to your computer and use it in GitHub Desktop.

Select an option

Save FreeFull/936f68d4d7410b5b89c1e4552fb6c318 to your computer and use it in GitHub Desktop.
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
Running `target/debug/tungstenite-test`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Io(Custom { kind: Other, error: "no current reactor" })', src/libcore/result.rs:1165:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
[package]
name = "tungstenite-test"
version = "0.1.0"
authors = ["Filip Szczepański <[email protected]>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
url = "2.1.0"
futures = "0.3.1"
[dependencies.tokio]
git = "https://github.com/tokio-rs/tokio"
[dependencies.tokio-tungstenite]
git = "https://github.com/dbcfd/tokio-tungstenite"
branch = "tokio2"
use std::time::Duration;
use futures::prelude::*;
use tokio_tungstenite as ws;
use ws::tungstenite::protocol::Message;
#[tokio::main]
async fn main() {
let url = url::Url::parse("wss://echo.websocket.org/").unwrap();
let (stream, _) = ws::connect_async(url).await.unwrap();
let (sink, stream) = stream.split();
tokio::spawn(get_messages(stream));
send_messages(sink).await;
}
async fn get_messages<Rx: Stream<Item = ws::tungstenite::Result<Message>> + Unpin>(mut stream: Rx) {
while let Some(message) = stream.next().await {
println!("{:?}", message);
}
}
async fn send_messages<Tx: Sink<Message> + Unpin>(mut sink: Tx) {
for _ in 0..3 {
sink.send(Message::Text("Hello!".into()))
.await.ok().expect("Sink unexpectedly closed.");
tokio::time::delay_for(Duration::from_secs(1)).await;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment