如何从 xml 文件流式传输连续更新

How to stream continuous updates from an xml file

示例文件:

<currencies><usd_gbp>0.80</usd_gbp><usd_eur>0.94<usd_eur></currencies>

我有一个 xml 文件 http://somesite.com/curency.xml,其中包含货币兑换率。该文件会定期更新。我希望流在更新发生时实时发出更改。您将如何创建一个 rx.js 可实时发出更新的可观察流?

到目前为止,我一直在查看这个示例,但是它会立即处理整个文件并结束。 http://schempy.com/2015/10/14/simple_async_with_rxjs/

如何让 steram 在文件发生更新时发出更新?我需要将 apiStream 合并到一个间隔还是有其他方法?

除非服务器可以向订阅者推送转换率更新的通知(例如通过 websockets),否则您将不得不使用常规 HTTP 更改请求定期轮询 xml 文件。

Rx.Observable.interval(500)
  .flatMap(() => getConversionRates())
  .subscribe(conversionRate => console.log(`conversionRate: ${conversionRate}`));

function getConversionRates() {
  // implement logic to do one call to the xml file
  // and emit all conversion rates as observable emissions
  Return Rx.Observable.from([{ currency: 'usd_gbp', conversion: 0.80}]) 
}

如果您真的关心完全相同货币+转换的重复排放,那么您可以使用 .groupBy(x => x.currency) 所有排放与 .distinctUntilChanged()