Cargo.toml
:
[package]
name = "sse"
version = "0.1.0"
edition = "2021"
[dependencies]
futures-util = "0.3.30"
poem = { version = "3.0.3", features = ["sse"] }
tokio = { version = "1.39.1", features = ["rt-multi-thread", "macros"] }
tokio-stream = "0.1.15"
tracing-subscriber = "0.3.18"
main.rs
:
use std::time::Instant;
use futures_util::StreamExt;
use poem::{
get, handler,
listener::TcpListener,
web::{
sse::{Event, SSE},
Html,
},
Route, Server,
};
use tokio::time::Duration;
#[handler]
fn index() -> Html<&'static str> {
Html(
r#"
<script>
let eventSource = new EventSource('event');
eventSource.onmessage = function(event) {
document.write("<div>" + event.data + "</div>");
}
</script>
"#,
)
}
#[handler]
fn event() -> SSE {
let now = Instant::now();
SSE::new(
tokio_stream::wrappers::IntervalStream::new(tokio::time::interval(Duration::from_secs(1)))
.map(move |_| Event::message(now.elapsed().as_secs().to_string())),
)
.keep_alive(Duration::from_secs(5))
}
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "poem=debug");
}
tracing_subscriber::fmt::init();
let app = Route::new().at("/", get(index)).at("/event", get(event));
Server::new(TcpListener::bind("0.0.0.0:8080"))
.run(app)
.await
}
标签:poem,use,tokio,sse,new,event,rust
From: https://www.cnblogs.com/soarowl/p/18320587