Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Err(NoWebsocketUpgrade) #3299

Open
2001lwy opened this issue Feb 27, 2024 · 1 comment
Open

Err(NoWebsocketUpgrade) #3299

2001lwy opened this issue Feb 27, 2024 · 1 comment

Comments

@2001lwy
Copy link

2001lwy commented Feb 27, 2024

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;
}

/// 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

@benediktms
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants