Giter Club home page Giter Club logo

Comments (11)

agalakhov avatar agalakhov commented on August 15, 2024

You're awaiting the writer before even giving the reader a chance to work. Of course this won't receive any bigger messages.

from tokio-tungstenite.

araraloren avatar araraloren commented on August 15, 2024

You're awaiting the writer before even giving the reader a chance to work. Of course this won't receive any bigger messages.

There two request in main. I added a connected! log. The second one is stuck at connect_async.
And I don't know what you mean awaiting.

from tokio-tungstenite.

agalakhov avatar agalakhov commented on August 15, 2024

You use the await keyword in your code. This makes the program stop and wait at the point. The WebSocket standard requires reaction on incoming messages, you can't just send and wait. Your code must start receiving before sending anything in order to be ready for incoming messages.

Just do receiving and sending at the same time, not after each other. And start receiving just after connecting. No sending before receiving is running.

from tokio-tungstenite.

araraloren avatar araraloren commented on August 15, 2024

You use the await keyword in your code. This makes the program stop and wait at the point. The WebSocket standard requires reaction on incoming messages, you can't just send and wait. Your code must start receiving before sending anything in order to be ready for incoming messages.

Just do receiving and sending at the same time, not after each other. And start receiving just after connecting. No sending before receiving is running.

No, it's not connect yet. The second one not output -- connected!.

from tokio-tungstenite.

agalakhov avatar agalakhov commented on August 15, 2024

What's the point of manually writing WebSocket-related HTTP headers including the key? Tungstenite does it for you. There is no need to over-engineer here. Just provide the URL.

from tokio-tungstenite.

agalakhov avatar agalakhov commented on August 15, 2024

Error 504 is definitely not a Tungstenite bug. It is server-side. It could be that Tungstenite just triggers it somehow.

from tokio-tungstenite.

daniel-abramov avatar daniel-abramov commented on August 15, 2024

@araraloren, after reading the issue and the discussion, I realized the whole "issue" could be reduced to a single line of code: connect_async("wss://www.itdog.cn/websockets").await with the question' Why does this fail for this particular server but does not fail for other test servers?' The rest of the code in the provided example is irrelevant.

The answer is: it depends, but oftentimes when we get issues like this, it turns out that the remote server expects specific headers to be set (authentication or whatnot). I would suggest that you look into the server's documentation. If the service provides a working test client (typically one written in JS/TS), check which headers are sent to see where it differs from Tungstenite. Unfortunately we can't do debugging of every third-party server.

I consider this issue closed as there is nothing to do from a library's standpoint. I hope that the provided information brings some clarity and helps you to get further!

PS: If you find an issue, feel free to open one, but please be specific. Sometimes, reading through issues containing a lot of irrelevant information is very time-consuming (especially if an ongoing discussion diverges).

from tokio-tungstenite.

araraloren avatar araraloren commented on August 15, 2024

What's the point of manually writing WebSocket-related HTTP headers including the key? Tungstenite does it for you. There is no need to over-engineer here. Just provide the URL.

Yes, I tried pass only url.

from tokio-tungstenite.

araraloren avatar araraloren commented on August 15, 2024

@araraloren, after reading the issue and the discussion, I realized the whole "issue" could be reduced to a single line of code: connect_async("wss://www.itdog.cn/websockets").await with the question' Why does this fail for this particular server but does not fail for other test servers?' The rest of the code in the provided example is irrelevant.

The answer is: it depends, but oftentimes when we get issues like this, it turns out that the remote server expects specific headers to be set (authentication or whatnot). I would suggest that you look into the server's documentation. If the service provides a working test client (typically one written in JS/TS), check which headers are sent to see where it differs from Tungstenite. Unfortunately we can't do debugging of every third-party server.

I consider this issue closed as there is nothing to do from a library's standpoint. I hope that the provided information brings some clarity and helps you to get further!

PS: If you find an issue, feel free to open one, but please be specific. Sometimes, reading through issues containing a lot of irrelevant information is very time-consuming (especially if an ongoing discussion diverges).

Thanks.
I've had success with reqwest-websocket which also base on tungstenite. The code is pretty much the same as tokio-tungstenite, but it's totally working. Maybe I am just not lucky with tokio-tungstenite.

pub async fn request_reqw(url: &str) -> color_eyre::Result<()> {
    let web_socket = reqwest_websocket::websocket(url).await?;

    println!("REQW: -- connected!");

    let (mut writer, mut reader) = web_socket.split();

    writer
        .send(reqwest_websocket::Message::Text(String::from(
            r#"{"task_id":"202407262337451c6sowxc36q865nb5p","task_token":"bd4c908a626dc75c"}"#,
        )))
        .await?;

The log:

REQW: -- connected!
REQW: Got message: Text("Request served by 7811941c69e658")
REQW: Got message: Text("{\"task_id\":\"202407262337451c6sowxc36q865nb5p\",\"task_token\":\"bd4c908a626dc75c\"}")
REQW: Done!
REQW: -- connected!
REQW: Got message: Text("{\"type\":\"finished\"}")
REQW: Done!
TOKIO: -- connected!
TOKIO: Got message: Text("Request served by 7811941c69e658")
TOKIO: Got message: Text("{\"task_id\":\"202407262337451c6sowxc36q865nb5p\",\"task_token\":\"bd4c908a626dc75c\"}")
TOKIO: Done!
Error:
   0: HTTP error: 504 Gateway Timeout

Location:
   src/main.rs:28

from tokio-tungstenite.

agalakhov avatar agalakhov commented on August 15, 2024

reqwest-websocket uses the same tungstenite internally. The server just doesn't like your headers for some reason. You can compare headers sent by reqwest-websocket with yours.

from tokio-tungstenite.

araraloren avatar araraloren commented on August 15, 2024

reqwest-websocket uses the same tungstenite internally. The server just doesn't like your headers for some reason. You can compare headers sent by reqwest-websocket with yours.

Thanks for your advices.

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.