简体   繁体   English

服务工作者与共享工作者

[英]Service Worker vs Shared Worker

What is the difference between Service Worker and Shared Worker? Service Worker 和 Shared Worker 有什么区别?

When should I use Service Worker instead of Shared Worker and vice versa?我什么时候应该使用 Service Worker 而不是 Shared Worker,反之亦然?

A service worker has additional functionality beyond what's available in shared workers, and once registered, they persist outside the lifespan of a given web page. Service Worker 具有共享 Worker 之外的其他功能,并且一旦注册,它们就会在给定网页的生命周期之外持续存在。

Service workers can respond to message events, like shared workers, but they also have access to additional events.服务工作者可以响应message事件,例如共享工作者,但他们也可以访问其他事件。 Handling fetch events allows service workers to intercept any network traffic (originating from a controlled page) and take specific actions, including serving responses from a Request / Response cache.处理fetch事件允许服务工作者拦截任何网络流量(源自受控页面)并采取特定操作,包括从Request / Response缓存中提供响应。 There are also plans to expose a push event to service workers, allowing web apps to receive push messages in the "background".计划向服务人员公开push事件,允许 Web 应用程序在“后台”接收推送消息。

The other major difference relates to persistence.另一个主要区别与持久性有关。 Once a service worker is registered for a specific origin and scope, it stays registered indefinitely.一旦为特定来源和范围注册了服务工作者,它就会无限期地保持注册状态。 (A service worker will automatically be updated if the underlying script changes, and it can be either manually or programmatically removed, but that's the exception.) Because a service worker is persistent, and has a life independent of the pages active in a web browser, it opens the door for things like using them to power the aforementioned push messaging—a service worker can be "woken up" and process a push event as long as the browser is running, regardless of which pages are active. (如果底层脚本发生变化,Service Worker 将自动更新,它可以手动或以编程方式删除,但这是例外。)因为 Service Worker 是持久的,并且其生命周期独立于 Web 浏览器中的活动页面,它为使用它们为上述推送消息提供动力之类的事情打开了大门——只要浏览器正在运行,服务工作者就可以“唤醒”并处理push事件,而不管哪些页面处于活动状态。 Future web platform features are likely to take advantage of this persistence as well.未来的 Web 平台功能也可能会利用这种持久性。

There are other, technical differences, but from a higher-level view, those are what stand out.还有其他技术差异,但从更高层次的角度来看,这些差异才是最突出的。

A SharedWorker context is a stateful session and is designed to multiplex web pages into a single app via asynchronous messaging (client/server paradigm). SharedWorker上下文是一个有状态的会话,旨在通过异步消息传递(客户端/服务器范式)将网页多路复用到单个应用程序中。 Its life cycle is domain based , rather than single page based like DedicatedWorker (two-tier paradigm).它的生命周期是基于域的,而不是像DedicatedWorker (两层范式)那样基于单页

A ServiceWorker context is designed to be stateless . ServiceWorker上下文被设计为无状态的。 It actually is not a persistent session at all - it is the inversion of control (IoC) or event-based persistence service paradigm.它实际上根本不是一个持久会话——它是控制反转(IoC) 或基于事件的持久服务范式。 It serves events, not sessions.它服务于事件,而不是会话。

One purpose is to serve concurrent secure asynchronous events for long running queries (LRQs) to databases and other persistence services (ie clouds).一个目的是为数据库和其他持久性服务(即云)提供用于长时间运行查询(LRQ) 的并发安全异步事件。 Exactly what a thread pool does in other languages.线程池在其他语言中的作用正是如此。

For example if your web app executes many concurrent secure LRQs to various cloud services to populate itself, ServiceWorkers are what you want.例如,如果您的 Web 应用程序对各种云服务执行许多并发安全 LRQ 以填充自身,那么ServiceWorkers就是您想要的。 You can execute dozens of secure LRQs instantly, without blocking the user experience.您可以立即执行数十个安全 LRQ,而不会阻碍用户体验。 SharedWorkers and DedicatedWorkers are not easily capable of handling many concurrent secure LRQs. SharedWorkersDedicatedWorkers不容易处理许多并发的安全 LRQ。 Also, some browsers do not support SharedWorkers .此外,一些浏览器不支持SharedWorkers

Perhaps they should have called ServiceWorkers instead: CloudWorkers for clarity, but not all services are clouds.也许他们应该改用ServiceWorkers :为清楚起见CloudWorkers ,但并非所有服务都是云。

Hopefully this explanation should lead you to thinking about how the various Worker types were designed to work together.希望这个解释能引导您思考各种 Worker 类型是如何设计为协同工作的。 Each has its own specialization, but the common goal is to reduce DOM latency and improve user experience in web based applications.每个都有自己的专长,但共同的目标是减少 DOM 延迟并改善基于 Web 的应用程序的用户体验。

Throw in some WebSockets for streaming and WebGL for graphics and you can build some smoking hot web apps that perform like multiplayer console games .加入一些用于流式传输的WebSockets和用于图形的WebGL ,您可以构建一些像多人控制台游戏一样运行的炙手可热的 Web 应用程序。

2022 06 Update 2022 06 更新

WebKit added support for the SharedWorker recently, see the details of the resolution in the issue link mentioned below. WebKit 最近增加了对SharedWorker的支持,解决方法的详细信息在下面提到的问题链接中。

2020 11 Update 2020 11 更新

Important detail for anyone interested in this discussion: SharedWorker is NOT supported by WebKit (was intentionally removed ~v6 or something).对本次讨论感兴趣的任何人的重要细节:WebKit支持SharedWorker (故意删除 ~v6 或其他内容)。

WebKit team are explicitly suggesting to use ServiceWorker wherever SharedWorker might seem relevant. WebKit 团队明确建议在与SharedWorker相关的任何地方使用ServiceWorker

For a community wish to get this functionality back to WebKit see this (unresolved as of now) issue.对于希望将此功能恢复到 WebKit 的社区,请参阅这个(目前尚未解决)问题。

Adding up to the previous great answers.加上以前的好答案。 As the main difference is that ServiceWorker is stateless (will shut down and then start with clear global scope) and SharedWorker will maintain state for the duration of the session.主要区别在于ServiceWorker是无状态的(将关闭然后以明确的全局范围启动),而SharedWorker将在会话期间保持状态。

Still there is a possibility to request that ServiceWorker will maintain state for the duration of a message handler.仍然有可能要求ServiceWorker在消息处理程序期间保持状态。

s.onmessage = e => e.waitUntil((async () => {
  // do things here
  // for example issue a fetch and store result in IndexedDb
  // ServiceWorker will live till that promise resolves
})())

The above code requires that the ServiceWorker will not shut down till the promise given as the parameter to waitUntil resolves.上面的代码要求ServiceWorker在作为参数提供给waitUntil的承诺解决之前不会关闭。 If many messages are handled concurrently in that manner ServiceWorker will not shut down untill all promises are resolved.如果以这种方式同时处理许多消息, ServiceWorker将不会关闭,直到所有承诺都得到解决。

This could be possibly used to prolong ServiceWorker life indefinitely making it effectively a SharedWorker .这可以用来无限期地延长ServiceWorker的生命,使其有效地成为SharedWorker Still, do keep in mind that browser might decide to force a shut down if ServiceWorker goes on fo too long.不过,请记住,如果ServiceWorker运行时间过长,浏览器可能会决定强制关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM