Comments (11)
You're await
ing the writer before even giving the reader a chance to work. Of course this won't receive any bigger messages.
from tokio-tungstenite.
You're
await
ing 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.
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.
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.
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.
Error 504 is definitely not a Tungstenite bug. It is server-side. It could be that Tungstenite just triggers it somehow.
from tokio-tungstenite.
@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.
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, 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.
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.
reqwest-websocket
uses the sametungstenite
internally. The server just doesn't like your headers for some reason. You can compare headers sent byreqwest-websocket
with yours.
Thanks for your advices.
from tokio-tungstenite.
Related Issues (20)
- 没有连接断开时,直接检测客户端的状态的api,以实现重新连接,这在其它语言中都是常见的
- Performance boost implementing `poll_write_vectored` and rustls 0.23 HOT 2
- Is MaybeTlsStream supposed to work for servers too? HOT 2
- 0.20 and 0.21 break something HOT 25
- Vectorized send HOT 2
- new rustls 0.23.x is incompatible with the 0.22.x used by tokio-tungstenite HOT 2
- Adding authorization header leads in infinite block of connect_async HOT 2
- How to correctly handle large messages HOT 1
- How can I send pong frames as heartbeats from a client? HOT 4
- Specify default-features = false for rustls in Cargo.toml
- Websocket missing some ping messages. HOT 2
- tokio-tungstenite not using version 0.22 of tungstenite HOT 1
- no process-level CryptoProvider available -- call CryptoProvider::install_default() before this point HOT 5
- No option to enable `url` flag on `tungstenite` HOT 1
- How to reply to the client and send a message if an error occures on `accept_async` funtion. HOT 1
- Rustls: no process-level CryptoProvider available -- call CryptoProvider::install_default() before this point HOT 3
- Websocket stops receiving messages unexpectedly HOT 3
- Is `Sink::send` cancellation safe? HOT 1
- Websocket stops receiving message unexpectedly HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tokio-tungstenite.