actix-web icon indicating copy to clipboard operation
actix-web copied to clipboard

Err(NoWebsocketUpgrade)

Open 2001lwy opened this issue 1 year ago • 1 comments

use actix::{Actor, StreamHandler}; use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer}; use actix_web_actors::ws;

/// Define HTTP actor struct MyWs;

impl Actor for MyWs { type Context = ws::WebsocketContext<Self>; }

/// Handler for ws::Message message impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWs { fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) { match msg { Ok(ws::Message::Ping(msg)) => ctx.pong(&msg), Ok(ws::Message::Text(text)) => ctx.text(text), Ok(ws::Message::Binary(bin)) => ctx.binary(bin), _ => (), } } }

pub async fn index(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> { println!("Request method: {}", req.method()); println!("Request URI: {}", req.uri());

println!("Request headers:");
for (header_name, header_value) in req.headers() {
    println!("{}: {}", header_name.as_str(), header_value.to_str().unwrap());
}
let resp = ws::start(MyWs {}, &req, stream);
println!("{:?}", resp);
resp

}

Request method: GET Request URI: /ws/ Request headers: host: 127.0.0.1:9000 connection: keep-alive expires: 1704532312062 user-agent: PostmanRuntime/7.32.1 accept: / authorization: 7142829946413649921 accept-encoding: gzip, deflate, br cache-control: no-cache postman-token: 86de83a2-0bf7-4a96-9206-5ad71e8c0908 access_token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjcxNDI4Mjk5NDY0MTM2NDk5MjEiLCJhY2NvdW50IjoiIiwicGVybWlzc2lvbnMiOltdLCJyb2xlX2lkcyI6W10sImV4cCI6MTcwNDUzMjMxMjA2Mn0.Ak5Wq2mzKzrQ_WQl8ZUTQTJ5UCYx9cjO336_hRRo2vI Err(NoWebsocketUpgrade)

I encountered this problem when connecting to Websocket and I can't solve it

2001lwy avatar Feb 27 '24 04:02 2001lwy

I'm encountering a similar issue, using essentially the code from the docs page, does not seem to work for me.

benediktms avatar Mar 16 '24 13:03 benediktms

As stated in the docs this error case is due to (Postman, in this case) not sending the correct Upgrade header to initiate a WebSocket.

robjtede avatar Jun 16 '24 22:06 robjtede