Skip to content

Commit

Permalink
Port/rename to async-std / async-tungstenite
Browse files Browse the repository at this point in the history
  • Loading branch information
sdroege committed Nov 12, 2019
1 parent 0c27799 commit b76d8cb
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 137 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
@@ -1,6 +1,8 @@
language: rust
rust:
- nightly-2019-09-05
- stable
- beta
- nightly

before_script:
- export PATH="$PATH:$HOME/.cargo/bin"
Expand Down
42 changes: 18 additions & 24 deletions Cargo.toml
@@ -1,30 +1,29 @@
[package]
name = "tokio-tungstenite"
description = "Tokio binding for Tungstenite, the Lightweight stream-based WebSocket implementation"
name = "async-tungstenite"
description = "async-std binding for Tungstenite, the Lightweight stream-based WebSocket implementation"
categories = ["web-programming::websocket", "network-programming", "asynchronous", "concurrency"]
keywords = ["websocket", "io", "web"]
authors = ["Daniel Abramov <dabramov@snapview.de>", "Alexey Galakhov <agalakhov@snapview.de>"]
authors = ["Sebastian Dröge <sebastian@centricular.com>"]
license = "MIT"
homepage = "https://github.com/snapview/tokio-tungstenite"
documentation = "https://docs.rs/tokio-tungstenite/0.9.0"
repository = "https://github.com/snapview/tokio-tungstenite"
version = "0.10.0-alpha.1"
homepage = "https://github.com/sdroege/async-tungstenite"
repository = "https://github.com/sdroege/async-tungstenite"
version = "0.1.0"
edition = "2018"

[features]
default = ["connect", "tls"]
connect = ["tokio-net", "stream"]
tls = ["tokio-tls", "native-tls", "stream", "tungstenite/tls"]
default = ["connect", "tls", "async_std_runtime"]
connect = ["stream"]
async_std_runtime = ["connect", "tls", "async-std"]
tls = ["async-tls", "stream"]
stream = ["bytes"]

[dependencies]
log = "0.4"
futures-preview = { version = "0.3.0-alpha.19", features = ["async-await"] }
futures = { version = "0.3", features = ["async-await"] }
pin-project = "0.4.0-alpha.9"
tokio-io = "0.2.0-alpha.6"

[dependencies.tungstenite]
#version = "0.9.1"
#version = "0.9.2"
git = "https://github.com/snapview/tungstenite-rs.git"
branch = "master"
default-features = false
Expand All @@ -33,20 +32,15 @@ default-features = false
optional = true
version = "0.4.8"

[dependencies.native-tls]
[dependencies.async-std]
optional = true
version = "0.2.0"
version = "1.0"

[dependencies.tokio-net]
[dependencies.async-tls]
optional = true
version = "0.2.0-alpha.6"
features = ["tcp"]

[dependencies.tokio-tls]
optional = true
version = "0.3.0-alpha.6"
version = "0.6.0"

[dev-dependencies]
tokio = "0.2.0-alpha.6"
url = "2.0.0"
env_logger = "0.6.1"
env_logger = "0.7"
async-std = { version = "1.0", features = ["attributes"] }
1 change: 1 addition & 0 deletions LICENSE
@@ -1,5 +1,6 @@
Copyright (c) 2017 Daniel Abramov
Copyright (c) 2017 Alexey Galakhov
Copyright (c) 2019 Sebastian Dröge

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
25 changes: 15 additions & 10 deletions README.md
@@ -1,26 +1,31 @@
# tokio-tungstenite
# async-tungstenite

Asynchronous WebSockets for Tokio stack.
Asynchronous WebSockets for [async-std](https://async.rs) and `std` `Future`s.

[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
[![Crates.io](https://img.shields.io/crates/v/tokio-tungstenite.svg?maxAge=2592000)](https://crates.io/crates/tokio-tungstenite)
[![Build Status](https://travis-ci.org/snapview/tokio-tungstenite.svg?branch=master)](https://travis-ci.org/snapview/tokio-tungstenite)
[![Crates.io](https://img.shields.io/crates/v/async-tungstenite.svg?maxAge=2592000)](https://crates.io/crates/async-tungstenite)
[![Build Status](https://travis-ci.org/sdroege/async-tungstenite.svg?branch=master)](https://travis-ci.org/sdroege/async-tungstenite)

[Documentation](https://docs.rs/tokio-tungstenite)
[Documentation](https://docs.rs/async-tungstenite)

## Usage

Add this in your `Cargo.toml`:

```toml
[dependencies]
tokio-tungstenite = "*"
async-tungstenite = "*"
```

Take a look at the `examples/` directory for client and server examples. You may also want to get familiar with
[tokio](https://tokio.rs/) if you don't have any experience with it.
[`async-std`](https://async.rs/) if you don't have any experience with it.

## What is tokio-tungstenite?
## What is async-tungstenite?

This crate is based on `tungstenite-rs` Rust WebSocket library and provides `tokio` bindings and wrappers for it, so you
can use it with non-blocking/asynchronous `TcpStream`s from and couple it together with other crates from `tokio` stack.
This crate is based on `tungstenite-rs` Rust WebSocket library and provides async-std bindings and wrappers for it, so you
can use it with non-blocking/asynchronous `TcpStream`s from and couple it together with other crates from the async-std stack.

## tokio-tungstenite

Originally this crate was created as a fork of [tokio-tungstenite](https://github.com/snapview/tokio-tungstenite)
and ported to [async-std](https://async.rs).
9 changes: 6 additions & 3 deletions examples/autobahn-client.rs
@@ -1,6 +1,6 @@
use futures::StreamExt;
use log::*;
use tokio_tungstenite::{connect_async, tungstenite::Result};
use async_tungstenite::{connect_async, tungstenite::Result};
use url::Url;

const AGENT: &'static str = "Tungstenite";
Expand Down Expand Up @@ -43,8 +43,7 @@ async fn run_test(case: u32) {
}
}

#[tokio::main]
async fn main() {
async fn run() {
env_logger::init();

let total = get_case_count().await.unwrap();
Expand All @@ -55,3 +54,7 @@ async fn main() {

update_reports().await.unwrap();
}

fn main() {
async_std::task::block_on(run());
}
15 changes: 9 additions & 6 deletions examples/autobahn-server.rs
@@ -1,8 +1,7 @@
use futures::StreamExt;
use log::*;
use std::net::{SocketAddr, ToSocketAddrs};
use tokio::net::{TcpListener, TcpStream};
use tokio_tungstenite::accept_async;
use async_std::net::{TcpListener, TcpStream, SocketAddr, ToSocketAddrs};
use async_tungstenite::accept_async;

async fn accept_connection(peer: SocketAddr, stream: TcpStream) {
let mut ws_stream = accept_async(stream).await.expect("Failed to accept");
Expand All @@ -17,12 +16,12 @@ async fn accept_connection(peer: SocketAddr, stream: TcpStream) {
}
}

#[tokio::main]
async fn main() {
async fn run() {
env_logger::init();

let addr = "127.0.0.1:9002"
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("Not a socket address");
Expand All @@ -37,6 +36,10 @@ async fn main() {
.expect("connected streams should have a peer address");
info!("Peer address: {}", peer);

tokio::spawn(accept_connection(peer, stream));
async_std::task::spawn(accept_connection(peer, stream));
}
}

fn main() {
async_std::task::block_on(run());
}
26 changes: 14 additions & 12 deletions examples/client.rs
Expand Up @@ -11,17 +11,17 @@
//! You can use this example together with the `server` example.

use std::env;
use std::io::{self, Write};

use futures::StreamExt;
use log::*;
use tungstenite::protocol::Message;

use tokio::io::AsyncReadExt;
use tokio_tungstenite::connect_async;
use async_std::prelude::*;
use async_std::io;
use async_std::task;
use async_tungstenite::connect_async;

#[tokio::main]
async fn main() {
async fn run() {
let _ = env_logger::try_init();

// Specify the server address to which the client will be connecting.
Expand All @@ -31,12 +31,10 @@ async fn main() {

let url = url::Url::parse(&connect_addr).unwrap();

// Right now Tokio doesn't support a handle to stdin running on the event
// loop, so we farm out that work to a separate thread. This thread will
// read data from stdin and then send it to the event loop over a standard
// futures channel.
// Spawn a new task that will will read data from stdin and then send it to the event loop over
// a standard futures channel.
let (stdin_tx, mut stdin_rx) = futures::channel::mpsc::unbounded();
tokio::spawn(read_stdin(stdin_tx));
task::spawn(read_stdin(stdin_tx));

// After the TCP connection has been established, we set up our client to
// start forwarding data.
Expand All @@ -61,15 +59,15 @@ async fn main() {
ws_stream.send(msg).await.expect("Failed to send request");
if let Some(msg) = ws_stream.next().await {
let msg = msg.expect("Failed to get response");
stdout.write_all(&msg.into_data()).unwrap();
stdout.write_all(&msg.into_data()).await.unwrap();
}
}
}

// Our helper method which will read data from stdin and send it along the
// sender provided.
async fn read_stdin(tx: futures::channel::mpsc::UnboundedSender<Message>) {
let mut stdin = tokio::io::stdin();
let mut stdin = io::stdin();
loop {
let mut buf = vec![0; 1024];
let n = match stdin.read(&mut buf).await {
Expand All @@ -80,3 +78,7 @@ async fn read_stdin(tx: futures::channel::mpsc::UnboundedSender<Message>) {
tx.unbounded_send(Message::binary(buf)).unwrap();
}
}

fn main() {
task::block_on(run())
}
20 changes: 13 additions & 7 deletions examples/server.rs
Expand Up @@ -23,8 +23,9 @@ use std::io::Error;
use futures::channel::mpsc::{UnboundedReceiver, UnboundedSender};
use futures::StreamExt;
use log::*;
use std::net::{SocketAddr, ToSocketAddrs};
use tokio::net::{TcpListener, TcpStream};
use async_std::task;
use async_std::net::{SocketAddr, ToSocketAddrs};
use async_std::net::{TcpListener, TcpStream};
use tungstenite::protocol::Message;

struct Connection {
Expand All @@ -50,7 +51,7 @@ async fn accept_connection(stream: TcpStream) {
.expect("connected streams should have a peer address");
info!("Peer address: {}", addr);

let mut ws_stream = tokio_tungstenite::accept_async(stream)
let mut ws_stream = async_tungstenite::accept_async(stream)
.await
.expect("Error during the websocket handshake occurred");

Expand All @@ -66,7 +67,7 @@ async fn accept_connection(stream: TcpStream) {
rx: msg_rx,
tx: response_tx,
};
tokio::spawn(handle_connection(c));
task::spawn(handle_connection(c));

while let Some(message) = ws_stream.next().await {
let message = message.expect("Failed to get request");
Expand All @@ -78,12 +79,13 @@ async fn accept_connection(stream: TcpStream) {
}
}
}
#[tokio::main]
async fn main() -> Result<(), Error> {

async fn run() -> Result<(), Error> {
let _ = env_logger::try_init();
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
let addr = addr
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("Not a socket address");
Expand All @@ -96,8 +98,12 @@ async fn main() -> Result<(), Error> {

while let Some(stream) = incoming.next().await {
let stream = stream.expect("Failed to accept stream");
tokio::spawn(accept_connection(stream));
task::spawn(accept_connection(stream));
}

Ok(())
}

fn main() -> Result<(), Error> {
task::block_on(run())
}
27 changes: 14 additions & 13 deletions examples/split-client.rs
Expand Up @@ -11,17 +11,16 @@
//! You can use this example together with the `server` example.

use std::env;
use std::io::{self, Write};

use futures::{SinkExt, StreamExt};
use log::*;
use tungstenite::protocol::Message;
use async_std::prelude::*;
use async_std::io;
use async_std::task;
use async_tungstenite::connect_async;

use tokio::io::AsyncReadExt;
use tokio_tungstenite::connect_async;

#[tokio::main]
async fn main() {
async fn run() {
let _ = env_logger::try_init();

// Specify the server address to which the client will be connecting.
Expand All @@ -31,12 +30,10 @@ async fn main() {

let url = url::Url::parse(&connect_addr).unwrap();

// Right now Tokio doesn't support a handle to stdin running on the event
// loop, so we farm out that work to a separate thread. This thread will
// read data from stdin and then send it to the event loop over a standard
// futures channel.
// Spawn a new task that will read data from stdin and then send it to the event loop over a
// standard futures channel.
let (stdin_tx, mut stdin_rx) = futures::channel::mpsc::unbounded();
tokio::spawn(read_stdin(stdin_tx));
task::spawn(read_stdin(stdin_tx));

// After the TCP connection has been established, we set up our client to
// start forwarding data.
Expand All @@ -62,15 +59,15 @@ async fn main() {
ws_tx.send(msg).await.expect("Failed to send request");
if let Some(msg) = ws_rx.next().await {
let msg = msg.expect("Failed to get response");
stdout.write_all(&msg.into_data()).unwrap();
stdout.write_all(&msg.into_data()).await.unwrap();
}
}
}

// Our helper method which will read data from stdin and send it along the
// sender provided.
async fn read_stdin(tx: futures::channel::mpsc::UnboundedSender<Message>) {
let mut stdin = tokio::io::stdin();
let mut stdin = io::stdin();
loop {
let mut buf = vec![0; 1024];
let n = match stdin.read(&mut buf).await {
Expand All @@ -81,3 +78,7 @@ async fn read_stdin(tx: futures::channel::mpsc::UnboundedSender<Message>) {
tx.unbounded_send(Message::binary(buf)).unwrap();
}
}

fn main() {
task::block_on(run())
}
2 changes: 1 addition & 1 deletion src/compat.rs
Expand Up @@ -3,7 +3,7 @@ use std::io::{Read, Write};
use std::pin::Pin;
use std::task::{Context, Poll};

use tokio_io::{AsyncRead, AsyncWrite};
use futures::io::{AsyncRead, AsyncWrite};
use tungstenite::{Error as WsError, WebSocket};

pub(crate) trait HasContext {
Expand Down

0 comments on commit b76d8cb

Please sign in to comment.