snapview / tokio-tungstenite

Future-based Tungstenite for Tokio. Lightweight stream-based WebSocket implementation
MIT License
1.88k stars 236 forks source link

Split makes it difficult to use #232

Closed MarcelCoding closed 2 years ago

MarcelCoding commented 2 years ago

Hi, I want to send and receive it independently. Therefore I need to split the connection. Unfortunately, the methods send and next are no longer available after the split. Is there anything similar that I could use or is this just a missing feature?

daniel-abramov commented 2 years ago

Hey, the aforementioned send() and next() methods are not part of the tokio-tungstenite, but the functions that we get from the trait extensions of SinkExt and StreamExt. After the split(), both counterparts implement the SinkExt and StreamExt correspondingly, so you should be able to use them 😉

MarcelCoding commented 2 years ago

Ok, first of all, thanks for your fast response. I looked a bit further into it and noticed that theoretically, the function exists but because of some trait bounds, I can't use it:

error[E0599]: the method `send` exists for struct `SplitStream<WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>>`, but its trait bounds were not satisfied
  --> zia-client\src\upstream\ws.rs:45:12
   |
45 |         wo.send(Message::binary(buf)).await.unwrap();
   |            ^^^^ method cannot be called on `SplitStream<WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>>` due to unsatisfied trait bounds
   |
  ::: C:\Users\Marcel\.cargo\registry\src\github.com-1ecc6299db9ec823\futures-util-0.3.21\src\stream\stream\split.rs:14:1
   |
14 | pub struct SplitStream<S>(BiLock<S>);
   | -------------------------------------
   | |
   | doesn't satisfy `_: SinkExt<_>`
   | doesn't satisfy `_: futures_util::Sink<_>`
   |
   = note: the following trait bounds were not satisfied:
           `SplitStream<WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>>: futures_util::Sink<_>`
           which is required by `SplitStream<WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>>: SinkExt<_>`

I especially don't understand this part:

   = note: the following trait bounds were not satisfied:
           `SplitStream<WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>>: futures_util::Sink<_>`
           which is required by `SplitStream<WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>>: SinkExt<_>`

If something is implementing Sink then it also automatically should be implementing SinkExt? Or may I am wonge here? I relay don't get it. image

Could it be that I am missing some features from some dependencies for that to work as expected?

daniel-abramov commented 2 years ago

Yeah right, but you should use SplitSink, not the SplitStream. SplitStream is the reading part 😉

MarcelCoding commented 2 years ago

Ohhhh...., thank you. Now it is working. Sorry, I just copied it from Tokyo (where the reading parts come first) and didn't check the types.