diff --git a/Cargo.toml b/Cargo.toml index 5ffe8c7..1a73ec5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ keywords = ["websocket", "io", "web"] authors = ["Daniel Abramov ", "Alexey Galakhov "] license = "MIT" homepage = "https://github.com/snapview/tokio-tungstenite" -documentation = "https://docs.rs/tokio-tungstenite/0.3.0" +documentation = "https://docs.rs/tokio-tungstenite/0.4.0" repository = "https://github.com/snapview/tokio-tungstenite" version = "0.4.0" @@ -21,8 +21,7 @@ futures = "0.1.14" tokio-io = "0.1.2" [dependencies.tungstenite] -git = "https://github.com/snapview/tungstenite-rs.git" # FIXME: set to 0.5.0 after tungstenite's approval -branch = "request_minor" +version = "0.5.0" default-features = false [dependencies.bytes] diff --git a/examples/server.rs b/examples/server.rs index 52005d1..d6dde95 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -60,7 +60,7 @@ fn main() { let connections_inner = connections.clone(); let handle_inner = handle.clone(); - accept_async(stream, None).and_then(move |ws_stream| { + accept_async(stream).and_then(move |ws_stream| { println!("New WebSocket connection: {}", addr); // Create a channel for our stream, which other sockets will use to diff --git a/src/lib.rs b/src/lib.rs index e9bfb3b..c42ac82 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,7 @@ use futures::{Poll, Future, Async, AsyncSink, Stream, Sink, StartSend}; use tokio_io::{AsyncRead, AsyncWrite}; use tungstenite::handshake::client::{ClientHandshake, Response, Request}; -use tungstenite::handshake::server::{ServerHandshake, Callback}; +use tungstenite::handshake::server::{ServerHandshake, Callback, NoCallback}; use tungstenite::handshake::{HandshakeRole, HandshakeError}; use tungstenite::protocol::{WebSocket, Message}; use tungstenite::error::Error as WsError; @@ -75,16 +75,26 @@ where /// 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 +where + S: AsyncRead + AsyncWrite, +{ + accept_hdr_async(stream, NoCallback) +} + +/// Accepts a new WebSocket connection with the provided stream. /// -/// You can also pass an optional `callback` which will -/// be called when the websocket request is received from an incoming client. -pub fn accept_async(stream: S, callback: Option) -> AcceptAsync +/// This function does the same as `accept_async()` but accepts an extra callback +/// for header processing. The callback receives headers of the incoming +/// requests and is able to add extra headers to the reply. +pub fn accept_hdr_async(stream: S, callback: C) -> AcceptAsync where S: AsyncRead + AsyncWrite, + C: Callback, { AcceptAsync { inner: MidHandshake { - inner: Some(server::accept(stream, callback)) + inner: Some(server::accept_hdr(stream, callback)) } } } @@ -145,11 +155,11 @@ impl Future for ConnectAsync { /// Future returned from accept_async() which will resolve /// once the connection handshake has finished. -pub struct AcceptAsync { - inner: MidHandshake>, +pub struct AcceptAsync { + inner: MidHandshake>, } -impl Future for AcceptAsync { +impl Future for AcceptAsync { type Item = WebSocketStream; type Error = WsError; diff --git a/tests/handshakes.rs b/tests/handshakes.rs index fdb0b3c..ba280c9 100644 --- a/tests/handshakes.rs +++ b/tests/handshakes.rs @@ -25,7 +25,7 @@ fn handshakes() { let connections = listener.incoming(); tx.send(()).unwrap(); let handshakes = connections.and_then(|(connection, _)| { - accept_async(connection, None).map_err(|e| io::Error::new(io::ErrorKind::Other, e)) + accept_async(connection).map_err(|e| io::Error::new(io::ErrorKind::Other, e)) }); let server = handshakes.for_each(|_| { Ok(())