1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-04-20 01:10:02 +02:00
dependabot[bot] 0132d3329f
build(deps): bump tokio from 1.44.0 to 1.44.2
Bumps [tokio](https://github.com/tokio-rs/tokio) from 1.44.0 to 1.44.2.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-1.44.0...tokio-1.44.2)

---
updated-dependencies:
- dependency-name: tokio
  dependency-version: 1.44.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-04-07 16:31:10 +00:00
..
2023-11-03 22:49:18 +00:00
2023-11-03 22:49:18 +00:00
2024-09-28 00:45:02 +00:00

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.recv().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.