Crates.io | actix-ws |
lib.rs | actix-ws |
version | 0.3.0 |
source | src |
created_at | 2020-04-25 19:25:44.068339 |
updated_at | 2024-07-20 06:42:27.446564 |
description | WebSockets for Actix Web, without actors |
homepage | https://actix.rs |
repository | https://github.com/actix/actix-extras |
max_upload_size | |
id | 234069 |
size | 84,134 |
actix-ws
WebSockets for Actix Web, without actors.
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(())
}
This project is licensed under either of
at your option.