Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatically reconnect websocket if connection reset #101

Closed
EverlastingBugstopper opened this issue May 11, 2020 · 1 comment
Closed

Comments

@EverlastingBugstopper
Copy link

i would expect the following code snippet to work fine:

    let (ws_stream, _) = connect_async(socket_url)
        .await
        .expect("Failed to connect to websocket");

    let (mut write, read) = ws_stream.split();

    read.for_each(|message| async {
            let message = message.unwrap();
            let message_text = message.into_text().unwrap();
            println!("{}", message_text);
    }

but after waiting for not that long, the thread will panic at the unwrap on let message = message.unwrap()

thread 'tokio-runtime-worker' panicked at 'called `Result::unwrap()` on an `Err` value: Protocol("Connection reset without closing handshake")'

i'd expect tokio-tungstenite to handle this and attempt a reconnect. maybe that's a flawed expectation - if so, do y'all have recommendations on how to handle that protocol message?

@agalakhov
Copy link
Member

We currently do not attempt to reconnect because we don't know what the disconnect means in the application. Sometimes disconnects are expected.

If there is no activity on TCP, the operating system will disconnect you sooner or later. If you don't have any better activity, send WebSocket ping messages periodically. This prevents disconnecting. If you do ping and still experience disconnects, this means network malfunction and is to be handled accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants