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 5421/week @ 2024-08-19 6409/week @ 2024-08-26 6224/week @ 2024-09-02 6674/week @ 2024-09-09 8527/week @ 2024-09-16 9053/week @ 2024-09-23 8177/week @ 2024-09-30 8871/week @ 2024-10-07 12109/week @ 2024-10-14 13284/week @ 2024-10-21 13603/week @ 2024-10-28 15157/week @ 2024-11-04 12063/week @ 2024-11-11 17740/week @ 2024-11-18 15878/week @ 2024-11-25 14048/week @ 2024-12-02

60,576 downloads per month
Used in 16 crates (13 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–25MB
~435K SLoC