From d55caee58ebdcc38067f33cbd55a5d1afd730e95 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 7 Aug 2022 23:12:52 +0100 Subject: [PATCH] update sse example --- Cargo.lock | 4 ++-- server-sent-events/Cargo.toml | 2 +- server-sent-events/src/broadcast.rs | 12 ++++++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 841aa65..0c54374 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -548,9 +548,9 @@ dependencies = [ [[package]] name = "actix-web-lab" -version = "0.16.8" +version = "0.16.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4caa655398cbfeecd57e639f997b62ee3090a4e88b974130035c2de41393363e" +checksum = "b8e25a9040595caff3abdf449185bcb846bd5da0001d916212bf370e9f146009" dependencies = [ "actix-files", "actix-http", diff --git a/server-sent-events/Cargo.toml b/server-sent-events/Cargo.toml index cd0a927..7ea2eea 100644 --- a/server-sent-events/Cargo.toml +++ b/server-sent-events/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" [dependencies] actix-web = "4" -actix-web-lab = "0.16.8" +actix-web-lab = "0.16.9" env_logger = "0.9" futures-util = { version = "0.3.17", default-features = false, features = ["std"] } log = "0.4" diff --git a/server-sent-events/src/broadcast.rs b/server-sent-events/src/broadcast.rs index b0b196e..15dbfb8 100644 --- a/server-sent-events/src/broadcast.rs +++ b/server-sent-events/src/broadcast.rs @@ -1,7 +1,7 @@ use std::{sync::Arc, time::Duration}; use actix_web::rt::time::interval; -use actix_web_lab::sse::{sse, Sse, SseSender}; +use actix_web_lab::sse::{sse, Sse, SseData, SseMessage, SseSender}; use futures_util::future; use parking_lot::Mutex; @@ -46,7 +46,11 @@ impl Broadcaster { let mut ok_clients = Vec::new(); for client in clients { - if client.comment("ping").await.is_ok() { + if client + .send(SseMessage::Comment("ping".into())) + .await + .is_ok() + { ok_clients.push(client.clone()); } } @@ -58,7 +62,7 @@ impl Broadcaster { pub async fn new_client(&self) -> Sse { let (tx, rx) = sse(10); - tx.data("connected").await.unwrap(); + tx.send(SseData::new("connected")).await.unwrap(); self.inner.lock().clients.push(tx); @@ -69,7 +73,7 @@ impl Broadcaster { pub async fn broadcast(&self, msg: &str) { let clients = self.inner.lock().clients.clone(); - let send_futures = clients.iter().map(|client| client.data(msg)); + let send_futures = clients.iter().map(|client| client.send(SseData::new(msg))); // try to send to all clients, ignoring failures // disconnected clients will get swept up by `remove_stale_clients`