mirror of
https://github.com/actix/actix-website
synced 2025-06-27 07:29:02 +02:00
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>
This commit is contained in:
@ -5,6 +5,6 @@ publish = false
|
||||
edition.workspace = true
|
||||
|
||||
[dependencies]
|
||||
actix = "0.13"
|
||||
actix-web = "4"
|
||||
actix-web-actors = "4.0.0"
|
||||
actix-ws = "0.3"
|
||||
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
|
||||
|
@ -1,36 +1,48 @@
|
||||
// <websockets>
|
||||
use actix::{Actor, StreamHandler};
|
||||
use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use actix_web_actors::ws;
|
||||
use actix_web::{rt, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use actix_ws::AggregatedMessage;
|
||||
use futures_util::StreamExt as _;
|
||||
|
||||
/// Define HTTP actor
|
||||
struct MyWs;
|
||||
async fn echo(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
|
||||
let (res, mut session, stream) = actix_ws::handle(&req, stream)?;
|
||||
|
||||
impl Actor for MyWs {
|
||||
type Context = ws::WebsocketContext<Self>;
|
||||
}
|
||||
let mut stream = stream
|
||||
.aggregate_continuations()
|
||||
// aggregate continuation frames up to 1MiB
|
||||
.max_continuation_size(2_usize.pow(20));
|
||||
|
||||
/// Handler for ws::Message message
|
||||
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWs {
|
||||
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
|
||||
match msg {
|
||||
Ok(ws::Message::Ping(msg)) => ctx.pong(&msg),
|
||||
Ok(ws::Message::Text(text)) => ctx.text(text),
|
||||
Ok(ws::Message::Binary(bin)) => ctx.binary(bin),
|
||||
_ => (),
|
||||
// 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();
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
async fn index(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
|
||||
let resp = ws::start(MyWs {}, &req, stream);
|
||||
println!("{:?}", resp);
|
||||
resp
|
||||
// respond immediately with response connected to WS session
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().route("/ws/", web::get().to(index)))
|
||||
HttpServer::new(|| App::new().route("/echo", web::get().to(echo)))
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
|
Reference in New Issue
Block a user