Firefox共享Web worker中的服务器发送事件



我试图在Firefox的共享Web Worker中使用EventSource(服务器发送的事件)。当我尝试在共享Worker的消息事件中使用EventSource构造函数时,Worker抛出错误ReferenceError: EventSource is not defined.

我认为这意味着这个对象不能在共享Web Worker上下文中访问,但我能够从谷歌Chrome中的共享Web Worker中访问EventSource。我想说清楚,我可以使用EventSource没有问题,如果直接从脚本中使用,我只是有问题,创建和使用它在Firefox内的共享Web工作者。

查看第4节中的EventSource规范,它指出:

当脚本的全局对象是Window对象或实现WorkerUtils接口的对象时,这个构造函数必须可见。

但是看看第5节的Web Workers规范,这里指定的接口是WorkerUtils接口。

interface WorkerGlobalScope : EventTarget {
    readonly attribute WorkerGlobalScope self;
    readonly attribute WorkerLocation location;
    void close();
    [TreatNonCallableAsNull] attribute Function? onerror;
    [TreatNonCallableAsNull] attribute Function? onoffline;
    [TreatNonCallableAsNull] attribute Function? ononline;
};
WorkerGlobalScope implements WorkerUtils;

根据两个规范中提到的信息,我假设我可以使用self.EventSourceEventSource从Worker中访问EventSource。然而,在Firefox中,情况似乎并非如此。

是否有一个原因,我不能从一个共享的Web工作者从Firefox访问EventSource ?

我想使用一个共享的Web Worker EventSource (Push Events),因为用户可以从有问题的网站打开多个选项卡,我想避免让浏览器打开多个EventSource连接到主机。

我应该使用另一种方法吗?不幸的是,Web Sockets不是一个选项,因为我正在ASP.net 2上开发。

感谢您的宝贵时间。

此错误已在Firefox Nightly中修复,并在Firefox 53中发布:

我已经将EventSource暴露给了dedicated worker和shared worker。

其他浏览器引擎实现这个吗?据我所知,Chrome支持专用worker、共享worker和service worker。Safari在专用worker中支持它。Edge不支持它。

向ServiceWorker公开EventSource:目前没有ServiceWorker的计划,因为有一个悬而未决的问题https://github.com/w3c/ServiceWorker/issues/947正在讨论中。

引用

    Mozilla Bug 1267903 - EventSource for workers
  • Firefox release: 53 - Workers and Service Workers

  • 未来版本|第一个知道即将发布的Firefox版本有什么新功能

  • 发布意图:EventSource for Dedicated Worker and Shared Worker - Google Groups

  • web-platform-tests/eventsource at master·w3c/web-platform-tests

最新更新