【发布时间】:2014-08-11 00:55:54
【问题描述】:
我正在尝试在 Firefox 的共享 Web Worker 中使用 EventSource(服务器发送事件)。当我尝试在 Shared Worker 的消息事件中使用 EventSource 构造函数时,Worker 会抛出错误ReferenceError: EventSource is not defined.
我认为这意味着无法在 Shared Web Worker 上下文中访问此对象,但我可以从 Google Chrome 中的 Shared Web Worker 中访问 EventSource。 我想明确一点,如果直接从脚本中使用,我可以毫无问题地使用 EventSource,我只是在 Firefox 的 Shared Web Worker 中创建和使用它时遇到了问题。
看着EventSource spec in Section 4 它说:
当脚本的全局对象是 Window 对象或实现 WorkerUtils 接口的对象时,此构造函数必须可见。
但是看着Web Workers spec in Section 5指定的接口有一个 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.EventSource 或仅在 Worker 中使用 EventSource 访问 EventSource。然而,在 Firefox 的实践中,情况似乎并非如此。
我无法从 Firefox 的 Shared Web Worker 访问 EventSource 有什么原因吗?
我想为 EventSource(推送事件)使用共享 Web Worker,因为用户可以从相关网站打开多个选项卡,并且我想避免浏览器打开多个 EventSource 到主机的连接。
我应该使用其他方法吗?不幸的是,Web Sockets 不是我在 ASP.net 2 上开发的选项。
感谢您的宝贵时间。
【问题讨论】:
-
你有没有找到解决这个问题的方法?我被困在同一个问题上,需要一个解决方案。感谢您的帮助。
-
目前的解决方案是使用polyfill,见Mike的问题How to make EventSource available inside SharedWorker in FireFox?
-
worker 中存在原生 EventSource 的 Firefox 漏洞:bugzilla.mozilla.org/show_bug.cgi?id=876498
标签: html firefox web-worker server-sent-events