mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
update sse example
This commit is contained in:
parent
05e19266ad
commit
d55caee58e
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -548,9 +548,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "actix-web-lab"
|
name = "actix-web-lab"
|
||||||
version = "0.16.8"
|
version = "0.16.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4caa655398cbfeecd57e639f997b62ee3090a4e88b974130035c2de41393363e"
|
checksum = "b8e25a9040595caff3abdf449185bcb846bd5da0001d916212bf370e9f146009"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-files",
|
"actix-files",
|
||||||
"actix-http",
|
"actix-http",
|
||||||
|
@ -5,7 +5,7 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
actix-web-lab = "0.16.8"
|
actix-web-lab = "0.16.9"
|
||||||
env_logger = "0.9"
|
env_logger = "0.9"
|
||||||
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
|
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::{sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
|
|
||||||
use actix_web::rt::time::interval;
|
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 futures_util::future;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
|
||||||
@ -46,7 +46,11 @@ impl Broadcaster {
|
|||||||
let mut ok_clients = Vec::new();
|
let mut ok_clients = Vec::new();
|
||||||
|
|
||||||
for client in clients {
|
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());
|
ok_clients.push(client.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,7 +62,7 @@ impl Broadcaster {
|
|||||||
pub async fn new_client(&self) -> Sse {
|
pub async fn new_client(&self) -> Sse {
|
||||||
let (tx, rx) = sse(10);
|
let (tx, rx) = sse(10);
|
||||||
|
|
||||||
tx.data("connected").await.unwrap();
|
tx.send(SseData::new("connected")).await.unwrap();
|
||||||
|
|
||||||
self.inner.lock().clients.push(tx);
|
self.inner.lock().clients.push(tx);
|
||||||
|
|
||||||
@ -69,7 +73,7 @@ impl Broadcaster {
|
|||||||
pub async fn broadcast(&self, msg: &str) {
|
pub async fn broadcast(&self, msg: &str) {
|
||||||
let clients = self.inner.lock().clients.clone();
|
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
|
// try to send to all clients, ignoring failures
|
||||||
// disconnected clients will get swept up by `remove_stale_clients`
|
// disconnected clients will get swept up by `remove_stale_clients`
|
||||||
|
Loading…
Reference in New Issue
Block a user