Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the event source polly could not fetch sse data but event source work #220

Open
jiangxiaoqiang opened this issue Sep 1, 2023 · 0 comments

Comments

@jiangxiaoqiang
Copy link

I have defined a rust sse api:

use actix_web::{App, HttpResponse, HttpServer, Responder, post, get};
use rust_wheel::common::util::net::sse_stream::SseStream;
use std::time::{Duration, SystemTime};
use tokio::{
    sync::mpsc::{UnboundedReceiver, UnboundedSender},
    task,
};

#[get("/sse/producer")]
pub async fn sse() -> impl Responder {
    let (tx, rx): (UnboundedSender<SSEMessage>, UnboundedReceiver<SSEMessage>) =
        tokio::sync::mpsc::unbounded_channel();
    task::spawn(async move {
        for _ in 0..5 {
            let message:SSEMessage = SSEMessage::from_data(&"data11".to_string(),&"TEX_LOG".to_string());
            tx.send(message).unwrap();
            tokio::time::sleep(Duration::from_secs(1)).await;
        }
    });
    let response = HttpResponse::Ok()
        .content_type("text/event-stream")
        .streaming(SseStream { receiver: Some(rx) });
    response
}

#[actix_web::main]
async fn main() -> Result<(), std::io::Error> {
    HttpServer::new(|| App::new().service(sse).service(sse))
        .bind("127.0.0.1:8000")?
        .run()
        .await
}

and this is my cargo.toml:

[package]
name = "rust-learn"
version = "0.1.0"
edition = "2018"

[dependencies]
tokio = { version = "1.17.0", features = ["full"] }
serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
actix-web = "4"
futures = "0.3"
eventsource = "0.5"
bytes = "1"
reqwest = { version = "0.11.20", features = ["stream"] }
tokio-stream = "0.1"
rust_wheel = { git = "https://github.com/jiangxiaoqiang/rust_wheel.git", branch = "diesel2.0" }
log4rs = "1.2.0"
log = "0.4.0"
actix-rt = "2.4.1"
futures-util = "0.3.28"

using this code to receive the data:

import React from 'react';
import { EventSourcePolyfill } from 'event-source-polyfill';

const App: React.FC = () => {

  React.useEffect(() => {
    doLocal();
  },[]);

  const doLocal = () => {
    let eventSource = new EventSourcePolyfill('/sse/producer');
    eventSource.onopen = () => {
      
    }
    eventSource.onerror = (error:any) => {
      console.log(error)
      eventSource.close();
    }
    eventSource.onmessage = (msg: any) => {
      console.log("origin polly:",msg)
    };
  }
}

export default App;

could not fetch any data but when I replace it with native event source like this:

const doOrigin = () => {
    let eventSource = new EventSource('/sse/producer');
    eventSource.onopen = () => {
      
    }
    eventSource.onerror = (error:any) => {
      console.log(error)
      eventSource.close();
    }
    eventSource.onmessage = (msg: any) => {
      console.log("origin:",msg)
    };
  }

it worked. why did this happen? Am I missing something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant