gpt4 book ai didi

javascript - 使用 MessageChannel HTML5 的 Web Workers 通信

转载 作者:技术小花猫 更新时间:2023-10-29 12:33:01 29 4
gpt4 key购买 nike

我想实现网络 worker 之间的通信。我阅读了 W3C 文档,发现 MessageChannel 是其中一种方法,但在阅读 MessageChannel 时,我无法理解如何使用 messagechannel 实现工作人员之间的通信。

我从 MSDN 上得到的

http://msdn.microsoft.com/en-in/library/ie/hh673525(v=vs.85).aspx

这里也没有合适的文档来做这件事。

我需要知道,如何使用 MessageChannel 与 webworker 进行通信?

这是 Demo抛出 DATA_CLONE_ERR

var worker = new Worker("sub1_worker.js");
worker.onmessage = function(e) {
$("#log").append("<br>" + e.data);
}
var channel = new MessageChannel();

worker.postMessage("ping", [channel.port2]);

channel.port1.onmessage = function(event) {
// Message is in event.data
alert("Message is: " + event.data);
}

channel.port1.postMessage('hello');



$("#send1").click(function() {
var msg = $("#msg").val();
if (msg && msg != "start")
worker.postMessage("ping2");

$("#msg").val("");

})
$("#send2").click(function() {
var msg = $("#msg").val();
if (msg && msg != "start")
worker.postMessage("ping3",[channel.port2]);
$("#msg").val("");

})

和 worker

onmessage = getMessage;

function getMessage(e){

if(e.ports[0])
e.ports[0].postMessage("msg from sub worker 1 "+ e.data);
else
postMessage("msg from sub worker 1 "+ e.data);
}

最佳答案

这里是纯 javascript 中关于如何在两个 worker 之间进行设置的干净示例:

在主线程中:

function setup(){
var channel = new MessageChannel();
var worker1 = new Worker("worker1.js");
var worker2 = new Worker("worker2.js");

// Setup the connection: Port 1 is for worker 1
worker1.postMessage({
command : "connect",
},[ channel.port1 ]);

// Setup the connection: Port 2 is for worker 2
worker2.postMessage({
command : "connect",
},[ channel.port2 ]);

worker1.postMessage({
command: "forward",
message: "this message is forwarded to worker 2"
});
}

worker1.js中:

var worker2port;
var onMessageFromWorker2 = function( event ){
console.log("Worker 1 received a message from worker 2: " + event.data);

//To send something back to worker 2
//worker2port.postMessage("");
};

self.onmessage = function( event ) {
switch( event.data.command )
{
// Setup connection to worker 2
case "connect":
worker2port = event.ports[0];
worker2port.onmessage = onMessageFromWorker2;
break;

// Forward messages to worker 2
case "forward":
// Forward messages to worker 2
worker2port.postMessage( event.data.message );
break;

//handle other messages from main
default:
console.log( event.data );
}
};

worker2.js

var worker1port;
var onMessageFromWorker1 = function( event ){
console.log("Worker 2 received a message from worker 1: " + event.data);

//To send something back to worker 1
//worker1port.postMessage("");
};

self.onmessage = function( event ) {
switch( event.data.command )
{
// Setup connection to worker 1
case "connect":
worker1port = event.ports[0];
worker1port.onmessage = onMessageFromWorker1;
break;

// Forward messages to worker 1
case "forward":
// Forward messages to worker 1
worker1port.postMessage( event.data.message );
break;

//handle other messages from main
default:
console.log( event.data );
}
};

这展示了如何处理来自主线程的消息,如何将消息从主线程转发到其他工作线程,以及如何在不涉及主线程的情况下直接在工作线程之间进行通信。

关于javascript - 使用 MessageChannel HTML5 的 Web Workers 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14191394/

29 4 0
文章推荐: css - 定位一个只有一个类值的元素
文章推荐: linux - 如何解释 numademo 输出
文章推荐: javascript - Angularjs - 有没有办法以编程方式点击 HTML