1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-23 15:51:06 +01:00
actix-extras/actix-ws
asonix b0d2947a4a
Add better support for receiving larger payloads (#430)
* Add better support for receiving larger payloads

This change enables the maximum frame size to be configured when receiving websocket frames. It also
adds a new stream time that aggregates continuation frames together into their proper collected
representation. It provides no mechanism yet for sending continuations.

* actix-ws: Add continuation & size config to changelog

* actix-ws: Add Debug, Eq to AggregatedMessage

* actix-ws: Add a configurable maximum size to aggregated continuations

* refactor: move aggregate types to own module

* test: fix chat example

* docs: update changelog

---------

Co-authored-by: Rob Ede <robjtede@icloud.com>
2024-07-20 06:09:30 +00:00
..
examples Add better support for receiving larger payloads (#430) 2024-07-20 06:09:30 +00:00
src Add better support for receiving larger payloads (#430) 2024-07-20 06:09:30 +00:00
Cargo.toml chore: share repo and website 2024-06-20 02:18:37 +01:00
CHANGELOG.md Add better support for receiving larger payloads (#430) 2024-07-20 06:09:30 +00:00
LICENSE-APACHE adopt actix-ws crate (#361) 2023-11-03 22:49:18 +00:00
LICENSE-MIT adopt actix-ws crate (#361) 2023-11-03 22:49:18 +00:00
README.md docs: clean up ws examples 2024-06-20 01:55:14 +01:00

Actix WS (Next Gen)

WebSockets for Actix Web, without actors.

crates.io Documentation Apache 2.0 or MIT licensed Dependency Status

Documentation & Resources

Usage

use actix_web::{middleware::Logger, web, App, HttpRequest, HttpServer, Responder};
use actix_ws::Message;

async fn ws(req: HttpRequest, body: web::Payload) -> actix_web::Result<impl Responder> {
    let (response, mut session, mut msg_stream) = actix_ws::handle(&req, body)?;

    actix_web::rt::spawn(async move {
        while let Some(Ok(msg)) = msg_stream.next().await {
            match msg {
                Message::Ping(bytes) => {
                    if session.pong(&bytes).await.is_err() {
                        return;
                    }
                }
                Message::Text(msg) => println!("Got text: {msg}"),
                _ => break,
            }
        }

        let _ = session.close(None).await;
    });

    Ok(response)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(move || {
        App::new()
            .wrap(Logger::default())
            .route("/ws", web::get().to(ws))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await?;

    Ok(())
}

License

This project is licensed under either of

at your option.