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 5211/week @ 2024-07-31 5231/week @ 2024-08-07 5832/week @ 2024-08-14 5938/week @ 2024-08-21 5841/week @ 2024-08-28 6778/week @ 2024-09-04 7146/week @ 2024-09-11 8788/week @ 2024-09-18 8483/week @ 2024-09-25 8920/week @ 2024-10-02 10410/week @ 2024-10-09 11562/week @ 2024-10-16 13486/week @ 2024-10-23 13908/week @ 2024-10-30 15042/week @ 2024-11-06 13454/week @ 2024-11-13

58,108 downloads per month
Used in 14 crates (11 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
~434K SLoC