[英]Issue with broadcast channel when using with nodejs worker_threads module
I am writing a nodejs script. 我正在编写一个nodejs脚本。 In that I have created a worker using worker_threads and a BroadcastChannel.
这样,我使用worker_threads和BroadcastChannel创建了一个worker。 I am not able to send message from my main thread to worker threads.
我无法将消息从主线程发送到工作线程。 However, I am able to send message from Worker to main thread.
但是,我能够将消息从Worker发送到主线程。
Following is my code for main.js 以下是我的main.js代码
let worker = new Worker('worker.js')
let channel = new BroadcastChannel('testChannel', {
type: 'node',
webWorkerSupport: true
})
channel.postMessage('sending message to worker')
channel.onmessage = message => {
console.log('received message in channel main')
console.log(message)
}
Following is the code in worker.js 以下是worker.js中的代码
let channel = new BroadcastChannel('testChannel', {
type: 'node',
webWorkerSupport: true
})
channel.onmessage = message => {
console.log('received message in channel')
console.log(message)
}
channel.postMessage('from worker')
`
You will need to add another BroadcastChannel
object for incoming messages. 您将需要为传入消息添加另一个
BroadcastChannel
对象。
Example (main.js): 示例(main.js):
let broadcastingChannel = new BroadcastChannel('testChannel', {
type: 'node',
webWorkerSupport: true
});
broadcastingChannel.postMessage('sending message to worker')
let incomingChannel = new BroadcastChannel('testChannel', {
type: 'node',
webWorkerSupport: true
});
incomingChannel.onmessage = message => {
console.log('received message in channel main')
console.log(message)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.