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

(to close or delete : wrong proposal) set timeout proposal on websocket connection #251

Open
mirandole opened this issue Oct 3, 2024 · 1 comment

Comments

@mirandole
Copy link

mirandole commented Oct 3, 2024

EDIT : This lead to blocking connection and can generate more issues than it solves

Hello,

To avoid the websocket to hang when something got wrong on the connection, I propose to set a timeout to the TCPStream:

//[...]
use std::time::Duration;
//use tungstenite::{connect, Message};
use tungstenite::Message;
use tungstenite::protocol::WebSocket;
use tungstenite::stream::MaybeTlsStream;
use tungstenite::handshake::client::Response;
use tungstenite::client_tls;
//[...]

impl<'a> FuturesWebSockets<'a> {
//[...]
    fn connect_wss(&mut self, wss: &str) -> Result<()> {
        let protocol_server_address: Vec<&str> = wss.split_inclusive("//").collect();
        let server_address: Vec<&str> = protocol_server_address[1].split_inclusive(".com").collect();
        let server_address = server_address[0];
        let server_address_with_port = server_address.to_owned() + ":443";

        let stream = TcpStream::connect(server_address_with_port).expect("Can't connect TcpStream");
        stream.set_read_timeout(Some(Duration::from_secs(20))).expect("Can't set timeout");

        match client_tls(Url::parse(wss)?, stream) {
            Ok(answer) => {
                self.socket = Some(answer);
                Ok(())
            }
            Err(e) => bail!(format!("Error during handshake {}", e)),
        }
    }

I'm very new to Rust, so my code could be improved. It's just a proposal to avoid hangs.

best regards

@mirandole mirandole changed the title set timeout proposal on websocket connection (to close or delete wrong: proposal) set timeout proposal on websocket connection Oct 22, 2024
@mirandole mirandole changed the title (to close or delete wrong: proposal) set timeout proposal on websocket connection (to close or delete : wrong proposal) set timeout proposal on websocket connection Oct 22, 2024
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
@mirandole and others