/// Start a thread to run the websocket event loop.
///
/// This will *block* until either an error occurs or the WS handshake succeeds.
Start a thread to run the websocket event loop.
This will *block* until either an error occurs or the WS handshake succeeds.
Start a thread to run the websocket event loop . This will * block * until either an error occurs or the WS handshake succeeds .
pub fn connect(
        url: String,
        mut session_id: Option<String>,
    ) -> ws::Result<(ws::Sender, Joiner)> {
        let (tx, rx) = mpsc::channel();
        let joiner = thread::named("WebSocketLogger", move || {
            struct Client<'a> {
                ws_tx: ws::Sender,
                tx: &'a Sender<ws::Result<ws::Sender>>,
                session_id: Option<String>,
            }
            impl<'a> Client<'a> {
                fn new(
                    ws_tx: ws::Sender,
                    tx: &'a Sender<ws::Result<ws::Sender>>,
                    session_id: Option<String>,
                ) -> Self {
                    Client {
                        ws_tx,
                        tx,
                        session_id,
                    }
                }
            }
            impl<'a> Handler for Client<'a> {
                // Include a "SessionId: <session-id>" header in our handshake request.
                fn build_request(&mut self, url: &Url) -> ws::Result<Request> {
                    let mut req = Request::from_url(url)?;
                    if let Some(ref session_id) = self.session_id {
                        req.headers_mut()
                            .push((SESSION_ID_HEADER.into(), session_id.clone().into()));
                    }
                    Ok(req)
                }
                fn on_open(&mut self, _: Handshake) -> ws::Result<()> {
                    if self.tx.send(Ok(self.ws_tx.clone())).is_err() {
                        Err(ws::Error {
                            kind: ws::ErrorKind::Internal,
                            details: From::from("Channel error - Could not send ws_tx."),
                        })
                    } else {
                        Ok(())
                    }
                }
                fn on_error(&mut self, _: ws::Error) {
                    // Ignore errors (to prevent `ws` from logging them).
                }
            }
            // Block indefinitely on the websocket's event loop.
            if let Err(e) = ws::connect(url, |ws_tx| Client::new(ws_tx, &tx, session_id.take())) {
                // Or, if an error occurs while connecting, notify the constructor above.
                let _ = tx.send(Err(e));
            }
        });
        match rx.recv() {
            Ok(Ok(ws_tx)) => Ok((ws_tx, joiner)),
            Ok(Err(e)) => Err(e),
            Err(e) => Err(ws::Error::new(
                ws::ErrorKind::Internal,
                format!("WebSocket Logger Error: {:?}", e),
            )),
        }
    }
pub fn connect ( url : String , mut session_id : Option < String > , ) -> ws :: Result < ( ws :: Sender , Joiner ) > { let ( tx , rx ) = mpsc :: channel ( ) ; let joiner = thread :: named ( " " , move | | { struct Client < ' a > { ws_tx : ws :: Sender , tx : & ' a Sender < ws :: Result < ws :: Sender > > , session_id : Option < String > , } impl < ' a > Client < ' a > { fn new ( ws_tx : ws :: Sender , tx : & ' a Sender < ws :: Result < ws :: Sender > > , session_id : Option < String > , ) -> Self { Client { ws_tx , tx , session_id , } } } impl < ' a > Handler for Client < ' a > { // Include a "SessionId: <session-id>" header in our handshake request. fn build_request ( & mut self , url : & Url ) -> ws :: Result < Request > { let mut req = Request :: from_url ( url ) ? ; if let Some ( ref session_id ) = self . session_id { req . headers_mut ( ) . push ( ( SESSION_ID_HEADER . into ( ) , session_id . clone ( ) . into ( ) ) ) ; } Ok ( req ) } fn on_open ( & mut self , _ : Handshake ) -> ws :: Result < ( ) > { if self . tx . send ( Ok ( self . ws_tx . clone ( ) ) ) . is_err ( ) { Err ( ws :: Error { kind : ws :: ErrorKind :: Internal , details : From :: from ( " " ) , } ) } else { Ok ( ( ) ) } } fn on_error ( & mut self , _ : ws :: Error ) { // Ignore errors (to prevent `ws` from logging them). } } // Block indefinitely on the websocket's event loop. if let Err ( e ) = ws :: connect ( url , | ws_tx | Client :: new ( ws_tx , & tx , session_id . take ( ) ) ) { // Or, if an error occurs while connecting, notify the constructor above. let _ = tx . send ( Err ( e ) ) ; } } ) ; match rx . recv ( ) { Ok ( Ok ( ws_tx ) ) => Ok ( ( ws_tx , joiner ) ) , Ok ( Err ( e ) ) => Err ( e ) , Err ( e ) => Err ( ws :: Error :: new ( ws :: ErrorKind :: Internal , format ! ( " " e ) , ) ) , } }
Start a thread to run the websocket event loop.
Start a thread to run the websocket event loop .
url
String
session_id
Option<String>