GithubHelp home page GithubHelp logo

Comments (5)

daniel-abramov avatar daniel-abramov commented on July 16, 2024 1

I guess it's neither tokio-tungstenite, nor tokio-timer, it's just that sometimes it's really a challenge to figure out where the Rust error comes from. I personally also think that it's quite annoying when such errors occur without a [visible] reason, if you want you can create an issue in tokio-timer repository and referenec this issue, maybe we'll get some insights which explain why exactly this code stopped compiling after adding tokio-timer. But it seems more like a general problem with the way Rust shows errors, sometimes it's extremely unobvious and the one has to spend a lot of time to figure out what's going on there.

Nevertheless, I found a solution for you which fixes this issue and makes the code compilable again. Here is the diff:

diff --git a/examples/server.rs b/examples/server.rs
index c757829..998c646 100644
--- a/examples/server.rs
+++ b/examples/server.rs
@@ -21,6 +21,7 @@ extern crate futures;
 extern crate tokio_core;
 extern crate tokio_tungstenite;
 extern crate tungstenite;
+extern crate tokio_timer;
 
 use std::cell::RefCell;
 use std::collections::HashMap;
@@ -28,13 +29,14 @@ use std::env;
 use std::io::{Error, ErrorKind};
 use std::rc::Rc;
 
-use futures::stream::Stream;
-use futures::Future;
-use tokio_core::net::TcpListener;
+use futures::stream::{Stream, SplitSink};
+use futures::{Future, Sink};
+use futures::sync::mpsc::UnboundedSender;
+use tokio_core::net::{TcpListener, TcpStream};
 use tokio_core::reactor::Core;
 use tungstenite::protocol::Message;
 
-use tokio_tungstenite::accept_async;
+use tokio_tungstenite::{accept_async, WebSocketStream};
 
 fn main() {
     let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
@@ -86,15 +88,14 @@ fn main() {
                                 .filter(|&(&k, _)| k != addr)
                                 .map(|(_, v)| v);
                 for tx in iter {
-                    tx.send(message.clone()).unwrap();
+                    (tx as &UnboundedSender<Message>).send(message.clone()).unwrap();
                 }
                 Ok(())
             });
 
             // Whenever we receive a string on the Receiver, we write it to
             // `WriteHalf<WebSocketStream>`.
-            let ws_writer = rx.fold(sink, |mut sink, msg| {
-                use futures::Sink;
+            let ws_writer = rx.fold(sink, |mut sink, msg| -> Result<SplitSink<WebSocketStream<TcpStream>>, ()> {
                 sink.start_send(msg).unwrap();
                 Ok(sink)
             });

from tokio-tungstenite.

daniel-abramov avatar daniel-abramov commented on July 16, 2024

@bbigras have you tried the workaround? (the server.rs code compiles well with it)

Did it help you to compile the code? (if so, feel free to close the issue)

from tokio-tungstenite.

compressed avatar compressed commented on July 16, 2024

@bbigras this has to do with UnboundedSender and Sink both exposing send fns. Here's the related issue: rust-lang/futures-rs#261.

from tokio-tungstenite.

daniel-abramov avatar daniel-abramov commented on July 16, 2024

@compressed this particular issue was caused by a fold() argument, after adding tokio-timer as external crate, Rust cannot make a type inference for the passed closure (don't know why).

I've also noticed the problem with UnboundedSender and Sink in the past and previously the example workarounded that by importing futures::Sink only when the Sink trait is actually used. But in my latest diff I decided to rewrite that part with UnboundedSender::send() explicitly. But I did not know that it's already being worked on and was discussed in futures-rs, so good to know :)

from tokio-tungstenite.

bbigras avatar bbigras commented on July 16, 2024

Did it help you to compile the code? (if so, feel free to close the issue)

Yes. Thanks.

from tokio-tungstenite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.