Update for Tungstenite 0.5.0

Signed-off-by: Alexey Galakhov <agalakhov@snapview.de>
pull/1/head
Alexey Galakhov 7 years ago
parent a5716829e7
commit 77d4d5b66f
  1. 5
      Cargo.toml
  2. 2
      examples/server.rs
  3. 26
      src/lib.rs
  4. 2
      tests/handshakes.rs

@ -6,7 +6,7 @@ keywords = ["websocket", "io", "web"]
authors = ["Daniel Abramov <dabramov@snapview.de>", "Alexey Galakhov <agalakhov@snapview.de>"] authors = ["Daniel Abramov <dabramov@snapview.de>", "Alexey Galakhov <agalakhov@snapview.de>"]
license = "MIT" license = "MIT"
homepage = "https://github.com/snapview/tokio-tungstenite" 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" repository = "https://github.com/snapview/tokio-tungstenite"
version = "0.4.0" version = "0.4.0"
@ -21,8 +21,7 @@ futures = "0.1.14"
tokio-io = "0.1.2" tokio-io = "0.1.2"
[dependencies.tungstenite] [dependencies.tungstenite]
git = "https://github.com/snapview/tungstenite-rs.git" # FIXME: set to 0.5.0 after tungstenite's approval version = "0.5.0"
branch = "request_minor"
default-features = false default-features = false
[dependencies.bytes] [dependencies.bytes]

@ -60,7 +60,7 @@ fn main() {
let connections_inner = connections.clone(); let connections_inner = connections.clone();
let handle_inner = handle.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); println!("New WebSocket connection: {}", addr);
// Create a channel for our stream, which other sockets will use to // Create a channel for our stream, which other sockets will use to

@ -31,7 +31,7 @@ use futures::{Poll, Future, Async, AsyncSink, Stream, Sink, StartSend};
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
use tungstenite::handshake::client::{ClientHandshake, Response, Request}; 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::handshake::{HandshakeRole, HandshakeError};
use tungstenite::protocol::{WebSocket, Message}; use tungstenite::protocol::{WebSocket, Message};
use tungstenite::error::Error as WsError; use tungstenite::error::Error as WsError;
@ -75,16 +75,26 @@ where
/// This is typically used after a socket has been accepted from a /// This is typically used after a socket has been accepted from a
/// `TcpListener`. That socket is then passed to this function to perform /// `TcpListener`. That socket is then passed to this function to perform
/// the server half of the accepting a client's websocket connection. /// the server half of the accepting a client's websocket connection.
pub fn accept_async<S>(stream: S) -> AcceptAsync<S, NoCallback>
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 /// This function does the same as `accept_async()` but accepts an extra callback
/// be called when the websocket request is received from an incoming client. /// for header processing. The callback receives headers of the incoming
pub fn accept_async<S>(stream: S, callback: Option<Callback>) -> AcceptAsync<S> /// requests and is able to add extra headers to the reply.
pub fn accept_hdr_async<S, C>(stream: S, callback: C) -> AcceptAsync<S, C>
where where
S: AsyncRead + AsyncWrite, S: AsyncRead + AsyncWrite,
C: Callback,
{ {
AcceptAsync { AcceptAsync {
inner: MidHandshake { inner: MidHandshake {
inner: Some(server::accept(stream, callback)) inner: Some(server::accept_hdr(stream, callback))
} }
} }
} }
@ -145,11 +155,11 @@ impl<S: AsyncRead + AsyncWrite> Future for ConnectAsync<S> {
/// Future returned from accept_async() which will resolve /// Future returned from accept_async() which will resolve
/// once the connection handshake has finished. /// once the connection handshake has finished.
pub struct AcceptAsync<S: AsyncRead + AsyncWrite> { pub struct AcceptAsync<S: AsyncRead + AsyncWrite, C: Callback> {
inner: MidHandshake<ServerHandshake<S>>, inner: MidHandshake<ServerHandshake<S, C>>,
} }
impl<S: AsyncRead + AsyncWrite> Future for AcceptAsync<S> { impl<S: AsyncRead + AsyncWrite, C: Callback> Future for AcceptAsync<S, C> {
type Item = WebSocketStream<S>; type Item = WebSocketStream<S>;
type Error = WsError; type Error = WsError;

@ -25,7 +25,7 @@ fn handshakes() {
let connections = listener.incoming(); let connections = listener.incoming();
tx.send(()).unwrap(); tx.send(()).unwrap();
let handshakes = connections.and_then(|(connection, _)| { 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(|_| { let server = handshakes.for_each(|_| {
Ok(()) Ok(())

Loading…
Cancel
Save