From 81e3e1b7b584f6876ae6841e052b05e734fc24eb Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Fri, 17 Mar 2017 18:25:29 +0100 Subject: [PATCH 1/3] Released version 0.1.0 Signed-off-by: Alexey Galakhov --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a383f18..be8c88d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,8 @@ repository = "https://github.com/snapview/tokio-tungstenite" version = "0.1.0" [dependencies] -futures = "*" -tokio-core = "*" -url = "*" +futures = "0.1.10" +tokio-core = "0.1.4" +url = "1.4.0" tungstenite = "0.1.0" From b621d9f213b01a016b99cd5c305e4f09a7eb2d65 Mon Sep 17 00:00:00 2001 From: pyros2097 Date: Tue, 21 Mar 2017 14:54:55 +0530 Subject: [PATCH 2/3] Fix example not working --- examples/server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/server.rs b/examples/server.rs index 73303bb..c757829 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -6,7 +6,7 @@ //! //! You can test this out by running: //! -//! cargo run --example server +//! cargo run --example server 127.0.0.1:12345 //! //! And then in another window run: //! From b2b77893c2334e3f45fab03ac603d4e8b89b266a Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Mon, 27 Mar 2017 18:25:19 +0200 Subject: [PATCH 3/3] Migrate to the tokio-io (fixes #1) --- Cargo.toml | 6 ++-- src/lib.rs | 75 +++++++-------------------------------------- tests/handshakes.rs | 53 ++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 66 deletions(-) create mode 100644 tests/handshakes.rs diff --git a/Cargo.toml b/Cargo.toml index be8c88d..ff117ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,9 @@ version = "0.1.0" [dependencies] futures = "0.1.10" -tokio-core = "0.1.4" +tokio-io = "0.1.1" +tungstenite = "0.1.0" url = "1.4.0" -tungstenite = "0.1.0" +[dev-dependencies] +tokio-core = "0.1.4" diff --git a/src/lib.rs b/src/lib.rs index fe754df..93d5ab5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,14 +21,14 @@ unused_import_braces)] extern crate futures; -extern crate tokio_core; +extern crate tokio_io; extern crate tungstenite; extern crate url; use std::io::ErrorKind; use futures::{Poll, Future, Async, AsyncSink, Stream, Sink, StartSend}; -use tokio_core::io::Io; +use tokio_io::{AsyncRead, AsyncWrite}; use url::Url; @@ -37,8 +37,7 @@ use tungstenite::handshake::server::ServerHandshake; use tungstenite::handshake::{HandshakeRole, HandshakeError}; use tungstenite::protocol::{WebSocket, Message}; use tungstenite::error::Error as WsError; -use tungstenite::client; -use tungstenite::server; +use tungstenite::{client, server}; /// Create a handshake provided stream and assuming the provided request. /// @@ -50,7 +49,7 @@ use tungstenite::server; /// /// This is typically used for clients who have already established, for /// example, a TCP connection to the remove server. -pub fn client_async(url: Url, stream: S) -> ConnectAsync { +pub fn client_async(url: Url, stream: S) -> ConnectAsync { ConnectAsync { inner: MidHandshake { inner: Some(client::client(url, stream)) @@ -69,7 +68,7 @@ pub fn client_async(url: Url, stream: S) -> ConnectAsync { /// This is typically used after a socket has been accepted from a /// `TcpListener`. That socket is then passed to this function to perform /// the server half of the accepting a client's websocket connection. -pub fn accept_async(stream: S) -> AcceptAsync { +pub fn accept_async(stream: S) -> AcceptAsync { AcceptAsync { inner: MidHandshake { inner: Some(server::accept(stream)) @@ -90,7 +89,7 @@ pub struct WebSocketStream { inner: WebSocket, } -impl Stream for WebSocketStream where T: Io { +impl Stream for WebSocketStream where T: AsyncRead + AsyncWrite { type Item = Message; type Error = WsError; @@ -99,7 +98,7 @@ impl Stream for WebSocketStream where T: Io { } } -impl Sink for WebSocketStream where T: Io { +impl Sink for WebSocketStream where T: AsyncRead + AsyncWrite { type SinkItem = Message; type SinkError = WsError; @@ -113,13 +112,13 @@ impl Sink for WebSocketStream where T: Io { } } -/// Future returned from connect_async() which will resolve +/// Future returned from client_async() which will resolve /// once the connection handshake has finished. pub struct ConnectAsync { inner: MidHandshake, } -impl Future for ConnectAsync { +impl Future for ConnectAsync { type Item = WebSocketStream; type Error = WsError; @@ -134,7 +133,7 @@ pub struct AcceptAsync { inner: MidHandshake, } -impl Future for AcceptAsync { +impl Future for AcceptAsync { type Item = WebSocketStream; type Error = WsError; @@ -147,7 +146,7 @@ struct MidHandshake { inner: Option, HandshakeError>>, } -impl Future for MidHandshake { +impl Future for MidHandshake { type Item = WebSocketStream; type Error = WsError; @@ -189,55 +188,3 @@ impl ToAsync for Result { } } -#[cfg(test)] -mod tests { - use super::*; - use url; - - use std::io; - - use futures::{Future, Stream}; - use tokio_core::net::{TcpStream, TcpListener}; - use tokio_core::reactor::Core; - - #[test] - fn handshakes() { - use std::sync::mpsc::channel; - use std::thread; - - let (tx, rx) = channel(); - - thread::spawn(move || { - let mut core = Core::new().unwrap(); - let handle = core.handle(); - let address = "0.0.0.0:12345".parse().unwrap(); - let listener = TcpListener::bind(&address, &handle).unwrap(); - let connections = listener.incoming(); - tx.send(()).unwrap(); - let handshakes = connections.and_then(|(connection, _)| { - accept_async(connection) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) - }); - let server = handshakes.for_each(|_| { - Ok(()) - }); - - core.run(server).unwrap(); - }); - - rx.recv().unwrap(); - let mut core = Core::new().unwrap(); - let handle = core.handle(); - let address = "0.0.0.0:12345".parse().unwrap(); - let tcp = TcpStream::connect(&address, &handle); - let handshake = tcp.and_then(|stream| { - let url = url::Url::parse("ws://localhost:12345/").unwrap(); - client_async(url, stream) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) - }); - let client = handshake.and_then(|_| { - Ok(()) - }); - core.run(client).unwrap(); - } -} diff --git a/tests/handshakes.rs b/tests/handshakes.rs new file mode 100644 index 0000000..becebb3 --- /dev/null +++ b/tests/handshakes.rs @@ -0,0 +1,53 @@ +extern crate futures; +extern crate tokio_core; +extern crate tokio_tungstenite; +extern crate url; + +use std::io; + +use futures::{Future, Stream}; +use tokio_core::net::{TcpStream, TcpListener}; +use tokio_core::reactor::Core; +use tokio_tungstenite::{client_async, accept_async}; + +#[test] +fn handshakes() { + use std::sync::mpsc::channel; + use std::thread; + + let (tx, rx) = channel(); + + thread::spawn(move || { + let mut core = Core::new().unwrap(); + let handle = core.handle(); + let address = "0.0.0.0:12345".parse().unwrap(); + let listener = TcpListener::bind(&address, &handle).unwrap(); + let connections = listener.incoming(); + tx.send(()).unwrap(); + let handshakes = connections.and_then(|(connection, _)| { + accept_async(connection) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) + }); + let server = handshakes.for_each(|_| { + Ok(()) + }); + + core.run(server).unwrap(); + }); + + rx.recv().unwrap(); + let mut core = Core::new().unwrap(); + let handle = core.handle(); + let address = "0.0.0.0:12345".parse().unwrap(); + let tcp = TcpStream::connect(&address, &handle); + let handshake = tcp.and_then(|stream| { + let url = url::Url::parse("ws://localhost:12345/").unwrap(); + client_async(url, stream) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) + }); + let client = handshake.and_then(|_| { + Ok(()) + }); + core.run(client).unwrap(); +} +