1
0
mirror of https://github.com/actix/actix-website synced 2024-11-23 16:31:08 +01: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:
dependabot[bot] 2024-08-19 01:21:54 +00:00 committed by GitHub
parent c2f760dbbe
commit ab7ce01131
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 85 deletions

View File

@ -1,22 +1,22 @@
--- ---
title: Websockets title: WebSockets
--- ---
import CodeBlock from "@site/src/components/code_block"; import CodeBlock from "@site/src/components/code_block";
# Websockets # WebSockets
Actix Web supports WebSockets with the `actix-web-actors` crate. It is possible to convert a request's `Payload` to a stream of [_ws::Message_][message] with a [_web::Payload_][payload] and then use stream combinators to handle actual messages, but it is simpler to handle websocket communications with an http actor. Actix Web supports a high-level WebSocket interface via the `actix-ws` crate. Using this crate, it's possible to convert a request's `Payload` stream into a stream of [_ws::Message_][message]s and then react to them inside a spawned async task.
The following is an example of a simple websocket echo server: The following is an example of a simple WebSocket echo server:
<CodeBlock example="websockets" file="main.rs" section="websockets" /> <CodeBlock example="websockets" file="main.rs" section="websockets" />
> A simple websocket echo server example is available in the [examples directory][examples]. > A simple WebSocket echo server example is available [in the examples repo][echo].
> An example chat server with the ability to chat over a websocket or TCP connection is available in [websocket-chat directory][chat] > An example chat server is also available [in the examples directory][chat]
[message]: https://docs.rs/actix-web-actors/2/actix_web_actors/ws/enum.Message.html [message]: https://docs.rs/actix-ws/0.3/actix_ws/enum.Message.html
[payload]: https://docs.rs/actix-web/4/actix_web/web/struct.Payload.html [payload]: https://docs.rs/actix-web/4/actix_web/web/struct.Payload.html
[examples]: https://github.com/actix/examples/tree/master/websockets [echo]: https://github.com/actix/examples/tree/master/websockets/echo-actorless
[chat]: https://github.com/actix/examples/tree/master/websockets/chat [chat]: https://github.com/actix/examples/tree/master/websockets/chat-actorless

62
examples/Cargo.lock generated
View File

@ -2,31 +2,6 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "actix"
version = "0.13.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de7fa236829ba0841304542f7614c42b80fca007455315c45c785ccfa873a85b"
dependencies = [
"actix-macros",
"actix-rt",
"actix_derive",
"bitflags 2.5.0",
"bytes",
"crossbeam-channel",
"futures-core",
"futures-sink",
"futures-task",
"futures-util",
"log",
"once_cell",
"parking_lot",
"pin-project-lite",
"smallvec",
"tokio",
"tokio-util",
]
[[package]] [[package]]
name = "actix-codec" name = "actix-codec"
version = "0.5.2" version = "0.5.2"
@ -260,24 +235,6 @@ dependencies = [
"url", "url",
] ]
[[package]]
name = "actix-web-actors"
version = "4.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "420b001bb709d8510c3e2659dae046e54509ff9528018d09c78381e765a1f9fa"
dependencies = [
"actix",
"actix-codec",
"actix-http",
"actix-web",
"bytes",
"bytestring",
"futures-core",
"pin-project-lite",
"tokio",
"tokio-util",
]
[[package]] [[package]]
name = "actix-web-codegen" name = "actix-web-codegen"
version = "4.3.0" version = "4.3.0"
@ -291,14 +248,17 @@ dependencies = [
] ]
[[package]] [[package]]
name = "actix_derive" name = "actix-ws"
version = "0.6.1" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c7db3d5a9718568e4cf4a537cfd7070e6e6ff7481510d0237fb529ac850f6d3" checksum = "a3a1fb4f9f2794b0aadaf2ba5f14a6f034c7e86957b458c506a8cb75953f2d99"
dependencies = [ dependencies = [
"proc-macro2", "actix-codec",
"quote", "actix-http",
"syn 2.0.66", "actix-web",
"bytestring",
"futures-core",
"tokio",
] ]
[[package]] [[package]]
@ -3344,9 +3304,9 @@ dependencies = [
name = "websockets" name = "websockets"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix",
"actix-web", "actix-web",
"actix-web-actors", "actix-ws",
"futures-util",
] ]
[[package]] [[package]]

View File

@ -5,6 +5,6 @@ publish = false
edition.workspace = true edition.workspace = true
[dependencies] [dependencies]
actix = "0.13"
actix-web = "4" actix-web = "4"
actix-web-actors = "4.0.0" actix-ws = "0.3"
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }

View File

@ -1,36 +1,48 @@
// <websockets> // <websockets>
use actix::{Actor, StreamHandler}; use actix_web::{rt, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer}; use actix_ws::AggregatedMessage;
use actix_web_actors::ws; use futures_util::StreamExt as _;
/// Define HTTP actor async fn echo(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
struct MyWs; let (res, mut session, stream) = actix_ws::handle(&req, stream)?;
impl Actor for MyWs { let mut stream = stream
type Context = ws::WebsocketContext<Self>; .aggregate_continuations()
} // aggregate continuation frames up to 1MiB
.max_continuation_size(2_usize.pow(20));
/// Handler for ws::Message message // start task but don't wait for it
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWs { rt::spawn(async move {
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) { // receive messages from websocket
while let Some(msg) = stream.next().await {
match msg { match msg {
Ok(ws::Message::Ping(msg)) => ctx.pong(&msg), Ok(AggregatedMessage::Text(text)) => {
Ok(ws::Message::Text(text)) => ctx.text(text), // echo text message
Ok(ws::Message::Binary(bin)) => ctx.binary(bin), session.text(text).await.unwrap();
_ => (),
} }
}
}
async fn index(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> { Ok(AggregatedMessage::Binary(bin)) => {
let resp = ws::start(MyWs {}, &req, stream); // echo binary message
println!("{:?}", resp); session.binary(bin).await.unwrap();
resp }
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] #[actix_web::main]
async fn main() -> std::io::Result<()> { 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))? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await