1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

chore: update actix-ws to v0.3

This commit is contained in:
Rob Ede 2024-07-20 07:55:03 +01:00
parent ea68e74373
commit 942e8ed173
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
6 changed files with 43 additions and 43 deletions

31
Cargo.lock generated
View File

@ -543,13 +543,14 @@ dependencies = [
[[package]] [[package]]
name = "actix-ws" name = "actix-ws"
version = "0.2.5" 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 = "535aec173810be3ca6f25dd5b4d431ae7125d62000aa3cbae1ec739921b02cf3" checksum = "a3a1fb4f9f2794b0aadaf2ba5f14a6f034c7e86957b458c506a8cb75953f2d99"
dependencies = [ dependencies = [
"actix-codec", "actix-codec",
"actix-http", "actix-http",
"actix-web", "actix-web",
"bytestring",
"futures-core", "futures-core",
"tokio", "tokio",
] ]
@ -8890,19 +8891,6 @@ dependencies = [
"rand", "rand",
] ]
[[package]]
name = "websocket-echo-actorless-example"
version = "1.0.0"
dependencies = [
"actix-files",
"actix-web",
"actix-ws",
"env_logger",
"futures-util",
"log",
"tokio",
]
[[package]] [[package]]
name = "websocket-example" name = "websocket-example"
version = "1.0.0" version = "1.0.0"
@ -8937,6 +8925,19 @@ dependencies = [
"tokio-util", "tokio-util",
] ]
[[package]]
name = "websockets-echo-actorless-example"
version = "1.0.0"
dependencies = [
"actix-files",
"actix-web",
"actix-ws",
"env_logger",
"futures-util",
"log",
"tokio",
]
[[package]] [[package]]
name = "which" name = "which"
version = "4.4.2" version = "4.4.2"

View File

@ -94,7 +94,7 @@ actix-utils = "3"
actix-web = "4.7" actix-web = "4.7"
actix-web-actors = "4.1" actix-web-actors = "4.1"
actix-web-lab = "0.20" actix-web-lab = "0.20"
actix-ws = "0.2.5" actix-ws = "0.3"
awc = "3.2" awc = "3.2"
chrono = { version = "0.4.30", features = ["serde"] } chrono = { version = "0.4.30", features = ["serde"] }

View File

@ -7,7 +7,6 @@ edition = "2021"
actix-files.workspace = true actix-files.workspace = true
actix-web.workspace = true actix-web.workspace = true
actix-ws.workspace = true actix-ws.workspace = true
env_logger.workspace = true env_logger.workspace = true
futures-util.workspace = true futures-util.workspace = true
log.workspace = true log.workspace = true

View File

@ -1,11 +1,14 @@
use std::time::{Duration, Instant}; use std::{
pin::pin,
time::{Duration, Instant},
};
use actix_ws::Message; use actix_ws::AggregatedMessage;
use futures_util::{ use futures_util::{
future::{select, Either}, future::{select, Either},
StreamExt as _, StreamExt as _,
}; };
use tokio::{pin, sync::mpsc, time::interval}; use tokio::{sync::mpsc, time::interval};
use crate::{ChatServerHandle, ConnId}; use crate::{ChatServerHandle, ConnId};
@ -20,7 +23,7 @@ const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);
pub async fn chat_ws( pub async fn chat_ws(
chat_server: ChatServerHandle, chat_server: ChatServerHandle,
mut session: actix_ws::Session, mut session: actix_ws::Session,
mut msg_stream: actix_ws::MessageStream, msg_stream: actix_ws::MessageStream,
) { ) {
log::info!("connected"); log::info!("connected");
@ -33,18 +36,21 @@ pub async fn chat_ws(
// unwrap: chat server is not dropped before the HTTP server // unwrap: chat server is not dropped before the HTTP server
let conn_id = chat_server.connect(conn_tx).await; let conn_id = chat_server.connect(conn_tx).await;
let msg_stream = msg_stream
.max_frame_size(128 * 1024)
.aggregate_continuations()
.max_continuation_size(2 * 1024 * 1024);
let mut msg_stream = pin!(msg_stream);
let close_reason = loop { let close_reason = loop {
// most of the futures we process need to be stack-pinned to work with select() // most of the futures we process need to be stack-pinned to work with select()
let tick = interval.tick(); let tick = pin!(interval.tick());
pin!(tick); let msg_rx = pin!(conn_rx.recv());
let msg_rx = conn_rx.recv();
pin!(msg_rx);
// TODO: nested select is pretty gross for readability on the match // TODO: nested select is pretty gross for readability on the match
let messages = select(msg_stream.next(), msg_rx); let messages = pin!(select(msg_stream.next(), msg_rx));
pin!(messages);
match select(messages, tick).await { match select(messages, tick).await {
// commands & messages received from client // commands & messages received from client
@ -52,30 +58,26 @@ pub async fn chat_ws(
log::debug!("msg: {msg:?}"); log::debug!("msg: {msg:?}");
match msg { match msg {
Message::Ping(bytes) => { AggregatedMessage::Ping(bytes) => {
last_heartbeat = Instant::now(); last_heartbeat = Instant::now();
// unwrap: // unwrap:
session.pong(&bytes).await.unwrap(); session.pong(&bytes).await.unwrap();
} }
Message::Pong(_) => { AggregatedMessage::Pong(_) => {
last_heartbeat = Instant::now(); last_heartbeat = Instant::now();
} }
Message::Text(text) => { AggregatedMessage::Text(text) => {
process_text_msg(&chat_server, &mut session, &text, conn_id, &mut name) process_text_msg(&chat_server, &mut session, &text, conn_id, &mut name)
.await; .await;
} }
Message::Binary(_bin) => { AggregatedMessage::Binary(_bin) => {
log::warn!("unexpected binary message"); log::warn!("unexpected binary message");
} }
Message::Close(reason) => break reason, AggregatedMessage::Close(reason) => break reason,
_ => {
break None;
}
} }
} }

View File

@ -66,7 +66,7 @@
<div id="log"></div> <div id="log"></div>
<form id="chatform"> <form id="chatform">
<input type="text" id="text" /> <input type="text" id="text" autocomplete="off" />
<input type="submit" id="send" /> <input type="submit" id="send" />
</form> </form>
@ -133,7 +133,7 @@
updateConnectionStatus() updateConnectionStatus()
} }
socket.onmessage = (ev) => { socket.onmessage = ev => {
log('Received: ' + ev.data, 'message') log('Received: ' + ev.data, 'message')
} }
@ -179,7 +179,7 @@
updateConnectionStatus() updateConnectionStatus()
}) })
$form.addEventListener('submit', (ev) => { $form.addEventListener('submit', ev => {
ev.preventDefault() ev.preventDefault()
const text = $input.value const text = $input.value
@ -192,7 +192,6 @@
}) })
updateConnectionStatus() updateConnectionStatus()
</script> </script>
</body> </body>
</html> </html>

View File

@ -1,5 +1,5 @@
[package] [package]
name = "websocket-echo-actorless-example" name = "websockets-echo-actorless-example"
version = "1.0.0" version = "1.0.0"
edition = "2021" edition = "2021"
@ -7,7 +7,6 @@ edition = "2021"
actix-files.workspace = true actix-files.workspace = true
actix-web.workspace = true actix-web.workspace = true
actix-ws.workspace = true actix-ws.workspace = true
env_logger.workspace = true env_logger.workspace = true
futures-util.workspace = true futures-util.workspace = true
log.workspace = true log.workspace = true