SharedWorker - Web APIs 编辑
The SharedWorker
interface represents a specific kind of worker that can be accessed from several browsing contexts, such as several windows, iframes or even workers. They implement an interface different than dedicated workers and have a different global scope, SharedWorkerGlobalScope
.
Note: If SharedWorker can be accessed from several browsing contexts, all those browsing contexts must share the exact same origin (same protocol, host and port).
Constructors
SharedWorker()
- Creates a shared web worker that executes the script at the specified URL.
Properties
Inherits properties from its parent, EventTarget
, and implements properties from AbstractWorker
.
AbstractWorker.onerror
- Is an
EventListener
that is called whenever anErrorEvent
of typeerror
bubbles through the worker. SharedWorker.port
Read only- Returns a
MessagePort
object used to communicate with and control the shared worker.
Methods
Inherits methods from its parent, EventTarget
, and implements methods from AbstractWorker
.
Example
In our Basic shared worker example (run shared worker), we have two HTML pages, each of which uses some JavaScript to perform a simple calculation. The different scripts are using the same worker file to perform the calculation — they can both access it, even if their pages are running inside different windows.
The following code snippet shows creation of a SharedWorker
object using the SharedWorker()
constructor. Both scripts contain this:
var myWorker = new SharedWorker('worker.js');
Both scripts then access the worker through a MessagePort
object created using the SharedWorker.port
property. If the onmessage event is attached using addEventListener, the port is manually started using its start()
method:
myWorker.port.start();
When the port is started, both scripts post messages to the worker and handle messages sent from it using port.postMessage()
and port.onmessage
, respectively:
first.onchange = function() {
myWorker.port.postMessage([first.value,second.value]);
console.log('Message posted to worker');
}
second.onchange = function() {
myWorker.port.postMessage([first.value,second.value]);
console.log('Message posted to worker');
}
myWorker.port.onmessage = function(e) {
result1.textContent = e.data;
console.log('Message received from worker');
}
Inside the worker we use the SharedWorkerGlobalScope.onconnect
handler to connect to the same port discussed above. The ports associated with that worker are accessible in the connect
event's ports
property — we then use MessagePort
start()
method to start the port, and the onmessage
handler to deal with messages sent from the main threads.
onconnect = function(e) {
var port = e.ports[0];
port.addEventListener('message', function(e) {
var workerResult = 'Result: ' + (e.data[0] * e.data[1]);
port.postMessage(workerResult);
});
port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter.
}
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'SharedWorker' in that specification. | Living Standard | No change from Unknown. |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论