Skip to content

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

@mirandole

Description

@mirandole

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions