1
0
mirror of https://github.com/actix/actix-website synced 2025-02-08 22:36:07 +01:00
dependabot[bot] ab7ce01131
chore(deps): bump actix-web-actors from 4.3.0 to 4.3.1+deprecated in /examples (#433)
* chore(deps): bump actix-web-actors in /examples

Bumps [actix-web-actors](https://github.com/actix/actix-web) from 4.3.0 to 4.3.1+deprecated.
- [Release notes](https://github.com/actix/actix-web/releases)
- [Changelog](https://github.com/actix/actix-web/blob/master/CHANGES.md)
- [Commits](https://github.com/actix/actix-web/compare/web-v4.3.0...web-v4.3.1)

---
updated-dependencies:
- dependency-name: actix-web-actors
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* docs: update websockets docs to use actix-ws

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
2024-08-19 01:21:54 +00:00

57 lines
1.7 KiB
Rust

// <websockets>
use actix_web::{rt, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use actix_ws::AggregatedMessage;
use futures_util::StreamExt as _;
async fn echo(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
let (res, mut session, stream) = actix_ws::handle(&req, stream)?;
let mut stream = stream
.aggregate_continuations()
// aggregate continuation frames up to 1MiB
.max_continuation_size(2_usize.pow(20));
// start task but don't wait for it
rt::spawn(async move {
// receive messages from websocket
while let Some(msg) = stream.next().await {
match msg {
Ok(AggregatedMessage::Text(text)) => {
// echo text message
session.text(text).await.unwrap();
}
Ok(AggregatedMessage::Binary(bin)) => {
// echo binary message
session.binary(bin).await.unwrap();
}
Ok(AggregatedMessage::Ping(msg)) => {
// respond to PING frame with PONG frame
session.pong(&msg).await.unwrap();
}
_ => {}
}
}
});
// respond immediately with response connected to WS session
Ok(res)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/echo", web::get().to(echo)))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
// </websockets>
// testing requires specific headers:
// Upgrade: websocket
// Connection: Upgrade
// Sec-WebSocket-Key: SOME_KEY
// Sec-WebSocket-Version: 13