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

问题描述 投票:0回答:1

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

为此,我正在使用此库https://github.com/jordanbyron/react-native-event-source

//.... 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);
    });
  }

这是我的日志不会打印任何内容。我期望每10秒在回调中记录一些数据。

react-native expo server-sent-events
1个回答
0
投票

RNEventSource适用于EXPO应用程序,我正在正确地使用它。也许您的问题出在您的$ {BASE_URL}中。如果您使用的是android studio模拟器,则应使用10.0.0.2而不是localhost(127.0.0.1)作为基本URL

为时已晚,但也许有一天能对某人有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.