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

actix_web_actors::ws::start don't accept Frame stream #3415

Open
alaravel opened this issue Jun 29, 2024 · 3 comments
Open

actix_web_actors::ws::start don't accept Frame stream #3415

alaravel opened this issue Jun 29, 2024 · 3 comments

Comments

@alaravel
Copy link

alaravel commented Jun 29, 2024

Hi
How I can pass awc stream to actix_web_actors::ws::start?
It's expected Result<Bytes, PayloadError> but framed is Result<Frame, ...>

let con=awc::Client::new()
        .ws(url)
        .connect().await;
   match con {
        Ok((mut res,mut framed)) => {
            let req=TestRequest::default()
                .insert_header(("sec-websocket-key",res.headers().get("sec-websocket-accept").unwrap()))
                .insert_header(("sec-websocket-version","13"))
                .insert_header(("connection","upgrade"))
                .insert_header(("upgrade","websocket"))
                .to_http_request();

            match actix_web_actors::ws::start(
                WsSession::new(id,room,addr),
                &req,
                framed //todo
            ){
                Ok(a) => {

                }
                Err(e) => {
                    println!("error ={:?}",e)
                }
            }
        }
        Err(_) => {}
    }

Your Environment

  • Rust Version (I.e, output of rustc -V):1.76.0
  • Actix Web Version:4.8.0
@earthskyorg
Copy link

To pass an awc stream to actix_web_actors::ws::start, you need to bridge the gap between the awc WebSocket client's frame-based protocol and the actix_web_actors::ws module's expectations.

@earthskyorg
Copy link

Here's a conceptual approach to solve this issue:

Capture Frames from awc: As you've done, capture the frames from the awc WebSocket connection.
Convert Frames to Bytes: Manually convert each frame into bytes. This involves encoding the frame according to the WebSocket protocol, which typically uses base64 encoding for binary data.
Pass Bytes to actix_web_actors::ws::start: Pass the converted bytes to actix_web_actors::ws::start.

@earthskyorg
Copy link

earthskyorg commented Aug 13, 2024

use actix_web::{web, App, HttpResponse};
use actix_web_actors::ws;
use futures_util::prelude::*;
use std::convert::TryInto;

async fn ws_handler(req: web::Path<(u32, u32)>, stream: web::Payload) -> impl IntoResponse {
    ws::start(req.into_inner(), &stream).await
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/ws/{id}/{room}", web::get().to(ws_handler))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

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