1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

update sse example

This commit is contained in:
Rob Ede 2022-08-07 23:12:52 +01:00
parent 05e19266ad
commit d55caee58e
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 11 additions and 7 deletions

4
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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`