3 <title>Passing MessagePort to a SharedWorker
</title>
4 <script src=
"worker_utils.js"></script>
8 document
.getElementById("result").innerHTML
+= message
+ "<br>";
12 var message_channel
= null;
15 worker
= new SharedWorker("messageport_worker.js");
16 worker
.port
.onmessage
= process_message_from_worker
;
19 function process_message_from_worker(event
) {
20 log("Message from worker: " + event
.data
);
21 if (event
.data
== "Worker connected.") {
22 log("Sending a port to the worker.");
23 message_channel
= new MessageChannel();
24 worker
.port
.postMessage({message
: "Sending a port.", port
: message_channel
.port2
}, [message_channel
.port2
]);
25 } else if (event
.data
== "Worker got a port.") {
26 // Communicate with the worker via the message channel.
27 message_channel
.port1
.onmessage = function (event
) {
28 log("Message from worker (via channel): " + event
.data
);
29 if (event
.data
== "Worker got a message via the passed port.")
34 message_channel
.port1
.start();
35 log("Sending a message to the worker via the channel.");
36 message_channel
.port1
.postMessage({message
: "Message via the channel."});
42 <body onload=
"do_test()">
43 <div id=statusPanel
></div>