use crate::prelude2::*;
use futures::stream::StreamExt;
use futures::SinkExt;
pub async fn channel_subscribe() -> HttpResponse {
let rx = create_channel().await;
HttpResponse::Ok()
.append_header(("Content-Type", "text/event-stream; charset=utf-8"))
.append_header(("Cache-Control", "no-store"))
.append_header(("X-Accel-Buffering", "no"))
.streaming(rx.map(
|message| -> core::result::Result<actix_web::web::Bytes, std::convert::Infallible> {
log::info!("{}", message);
Ok(actix_web::web::Bytes::from(message)) },
))
}
pub async fn create_channel() -> futures::channel::mpsc::UnboundedReceiver<String> {
let (mut tx, rx) = futures::channel::mpsc::unbounded::<String>();
let _ = tx.send(String::from("Channel Connected")).await;
tokio::spawn(async move {
loop {
match tx.send(String::from("hellol啊啦啦啦啦")).await {
Ok(()) => {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
Err(e) => {
log::warn!("hello: error={:?}", e.to_string());
break;
}
}
}
});
rx
}