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 6b04450703
Enable sending Continuations from actix-ws (#431)
* Enable sending continuations from an actix-ws Session

* actix-ws: Allow sending continuations from Session

* Convert ignored doctests to no_run doctests

---------

Co-authored-by: Rob Ede <robjtede@icloud.com>
2024-05-13 16:49:34 +00:00
..
examples chore: bump futures-* deps 2024-03-02 21:29:40 +00:00
src Enable sending Continuations from actix-ws (#431) 2024-05-13 16:49:34 +00:00
Cargo.toml chore: bump futures-* deps 2024-03-02 21:29:40 +00:00
CHANGELOG.md Enable sending Continuations from actix-ws (#431) 2024-05-13 16:49:34 +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 chore: fmt markdowns 2024-01-06 21:08:09 +00: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

# Cargo.toml
anyhow = "1"
actix-web = "4"
actix-ws-ng = "0.3"
// main.rs
use actix_web::{middleware::Logger, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use actix_ws::Message;

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

    actix_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(s) => println!("Got text, {}", s),
                _ => break,
            }
        }

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

    Ok(response)
}

#[actix_web::main]
async fn main() -> Result<(), anyhow::Error> {
    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.