10 releases

0.3.0 Jul 20, 2024
0.2.5 Mar 10, 2022
0.1.0 Sep 27, 2020
0.1.0-alpha.1 May 8, 2020
0.1.0-alpha.0 Apr 25, 2020

#7 in WebSocket

Download history 11832/week @ 2024-10-19 14787/week @ 2024-10-26 15307/week @ 2024-11-02 11903/week @ 2024-11-09 17235/week @ 2024-11-16 14896/week @ 2024-11-23 15645/week @ 2024-11-30 17122/week @ 2024-12-07 19531/week @ 2024-12-14 11187/week @ 2024-12-21 10250/week @ 2024-12-28 16402/week @ 2025-01-04 16617/week @ 2025-01-11 19231/week @ 2025-01-18 24110/week @ 2025-01-25 18807/week @ 2025-02-01

81,402 downloads per month
Used in 23 crates (18 directly)

MIT/Apache

27KB
421 lines

actix-ws

WebSockets for Actix Web, without actors.

crates.io Documentation Version MIT or Apache 2.0 licensed
Dependency Status Download Chat on Discord

Example

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(())
}

Resources

License

This project is licensed under either of

at your option.

Dependencies

~15–26MB
~443K SLoC