React native 如何在React Native中侦听服务器端事件?

React native 如何在React Native中侦听服务器端事件?,react-native,expo,server-sent-events,React Native,Expo,Server Sent Events,我正在我的react本机应用程序上设置服务器发送的事件(使用expo托管流)。但它不起作用 为此,我正在使用这个库 问题是我的日志不会打印任何东西。我希望每隔10秒在回调中记录一些数据。rEventSource适用于expo应用程序,我正在使用它。也许你的问题出在你的${BASE\u URL}。如果您使用的是android studio emulator,那么应该使用10.0.0.2而不是localhost(127.0.0.1)作为基本URL 虽然为时已晚,但也许有一天它会帮助别人 我正在使用S

我正在我的react本机应用程序上设置服务器发送的事件(使用expo托管流)。但它不起作用

为此,我正在使用这个库


问题是我的日志不会打印任何东西。我希望每隔10秒在回调中记录一些数据。

rEventSource适用于expo应用程序,我正在使用它。也许你的问题出在你的${BASE\u URL}。如果您使用的是android studio emulator,那么应该使用10.0.0.2而不是localhost(127.0.0.1)作为基本URL

虽然为时已晚,但也许有一天它会帮助别人


我正在使用Symfony php和Mercure Bundle作为我的后端,React用于我的web视图,React native/expo用于我的移动版本。

我终于成功地使它工作了。我必须在addEventListener方法中指定事件名称,如下所示:
this.eventSource.addEventListener('myEventName',(data)=>{console.log('startedthewatch',data);}myEventName应该是什么?我在浏览器中使用“消息”,但在这里似乎不起作用@浏览器中的ceessage尝试使用“onmessage”方法而不是“addEventListener”,如:const eventSource=new eventSource(yourURL);eventSource.onmessage=e=>{console.log(e)}
//.... imports here
  getTodayMissions() {
    ...
    const options = { headers: { Authorization: `Bearer ${token}` } };
    const eventsurl = `${BASE_URL}/server-events/`;
    const eventSource = new RNEventSource(eventsurl, options);
    const url = `missionstodayapp?userId=${user.id}`;


    console.log('start the watch', eventsurl, url);
    this.eventSource.addEventListener(url, (data) => {
      console.log('started the watch', data);
    });
  }