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

#8 in WebSocket

Download history 6623/week @ 2024-07-18 5646/week @ 2024-07-25 5099/week @ 2024-08-01 5352/week @ 2024-08-08 5865/week @ 2024-08-15 6177/week @ 2024-08-22 5834/week @ 2024-08-29 6659/week @ 2024-09-05 7167/week @ 2024-09-12 9073/week @ 2024-09-19 8633/week @ 2024-09-26 8756/week @ 2024-10-03 10886/week @ 2024-10-10 11338/week @ 2024-10-17 14498/week @ 2024-10-24 11304/week @ 2024-10-31

49,404 downloads per month
Used in 13 crates (10 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
~433K SLoC