rwf2 / Rocket

A web framework for Rust.
https://rocket.rs
Other
23.59k stars 1.52k forks source link

Update websocket.rs #2751

Closed gaoyia closed 2 months ago

gaoyia commented 2 months ago

return accept_key

gaoyia commented 2 months ago

I am using rocket_ws. I need to fetch the Token from the request header and calculate the value of accept_key. How should I do this?

#[get("/echo")]
fn echo(ws: rocket_ws::WebSocket) -> rocket_ws::Stream!['static] {
    let get_accept_key = ws.get_accept_key();
    let request = ....request;

    rocket_ws::Stream! { ws =>
        for await message in ws {
            yield message?;
        }
    }
}
SergioBenitez commented 2 months ago

Why do you need the Sec-WebSocket-Key? I'm not opposed to exposing it, as there is no security implication, but the key should be a likely protocol-level concern, so it's curious that you would need the value.

gaoyia commented 2 months ago

I personally prefer using it as a connection ID and manually maintaining the uniqueness of Sec-WebSocket-Key.

I also want to know how to get the "Request" in WebSocket.

thanks a lot.

SergioBenitez commented 2 months ago

Merged.