ws-rs icon indicating copy to clipboard operation
ws-rs copied to clipboard

Send headers with websocket connection

Open shreyaslad opened this issue 6 years ago • 1 comments

I'm trying to send custom headers with the websocket connection but I can't figure out how. There was a previous issue that mentioned this (#43), however, all the links given in that issue are dead. Can someone show me how to send headers to an open websocket connection?

shreyaslad avatar Jan 18 '19 14:01 shreyaslad

For client handler overwrite build_request:

fn build_request(&mut self, url: &url::Url) -> ws::Result<Request> { let mut req = Request::from_url(url)?;

    req.headers_mut().push(("TheHeader".into(), "That's it".into()));
   
    Ok(req)

}

For server I think you can do same thing inside on_open (Should be checked though):

fn on_open(&mut self, hs: Handshake) -> ws::Result<()> { hs.req.headers_mut().push(("TheHeader".into(), "That's it".into())); }

madmaxio avatar Feb 18 '19 10:02 madmaxio