snapview / tokio-tungstenite

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

handshake http 400 #267

Closed smeana closed 1 year ago

smeana commented 1 year ago

Hello

I am unable to connect to wss (endpoint is working fine as I am connected from a java client) with the following code:

use log::{debug, info};
use url::Url;
use tokio_tungstenite::connect_async;

#[tokio::main(flavor = "multi_thread", worker_threads = 4)]
async fn main() {
    let (ws, response) = 
        connect_async(Url::parse("wss://stream.com:6787/ws/")
        .unwrap())
        .await
        .expect("Can't connect");

I had the following issue:

thread 'main' panicked at 'Can't connect: Http(Response { status: 400, version: HTTP/1.1, headers: {"date": "Wed, 08 Mar 2023 13:18:27 GMT", "content-type": "text/plain", "content-length": "20", "connection": "keep-alive"}, body: None })', src\main.rs:12:10

cargo.toml

[dependencies]
tokio = { version = "1.26.0", features = ["full"] }
tungstenite = "0.18.0"
tokio-tungstenite = { version ="0.17.2", features = ["native-tls"] }
url = "2.3.1"
log = "0.4.17"
agalakhov commented 1 year ago

Check your server. It seems not to accept WebSocket connections at the given port.

smeana commented 1 year ago

@agalakhov Thanks, It is sorted now was a version issue.

Does tokio-tungstenite in build functionality to handle ping pong ?

daniel-abramov commented 1 year ago

Does tokio-tungstenite in build functionality to handle ping pong and reconnection in case of server failure? any example to manage this?

Automatic handling of pings/pongs is part of the RFC, so tungstenite supports it out-of-the-box.

WebSocket does not have a mechanism of some sort of a fast recovery/reconnection, so reconnection is just establishing a new connection after the first one was closed, so we don't do automatic reconnects. You can reconnect by just re-establishing the connection once your existing connection stops.