📜  Node.js MessageChannel.postMessage() 方法

📅  最后修改于: 2022-05-13 01:56:22.827000             🧑  作者: Mango

Node.js MessageChannel.postMessage() 方法

MessagePort.postMessage()方法是worker_threads模块中Worker 类的内置应用程序编程接口,用于将消息从一个端口发送到另一个端口。

句法:

const MessagePort.postMessage(value[, transferList])

参数:此方法将值作为参数,可以包含任何类型的对象。

返回值:此方法将消息从一个端口发送到另一个端口。

示例 1:文件名:index.js

Javascript
// Node.js program to demonstrate the
// MessageChannel.postMessage() method
 
// Importing worker_thread module
const { MessageChannel, receiveMessageOnPort }
        = require('worker_threads');
 
// Creating and initializing the MessageChannel
const { port1, port2 } = new MessageChannel();
 
// Sending data to port 2
// by using postMessage() method
port1.postMessage({ hello: 'world1' });
 
// Posting data in port 1
// by using postMessage() method
port2.postMessage({ hello: 'world2' });
 
/// Display the result
console.log("received data in port1 : ");
console.log(receiveMessageOnPort(port1));
 
console.log("received data in port2 : ");
console.log(receiveMessageOnPort(port2));
 
// Closing the ports
port1.close();
port2.close();


Javascript
// Node.js program to demonstrate the
// MessageChannel.postMessage() method
 
// Importing worker_thread module
 
const { MessageChannel, receiveMessageOnPort }
        = require('worker_threads');
 
// Creating and initializing the MessageChannel
const { port1, port2 } = new MessageChannel();
 
// Catching the event message
port2.on('message', (message) => console.log(message));
 
// Catching the event close
port2.on('close', () => console.log('closed!'));
 
// Sending message to port2
// by using postMessage() method
port1.postMessage('GFG');
 
// Closing port by using
// close() method
port1.close();


输出:

received data in port1 :
{ message: { hello: 'world2' } }
received data in port2 :
{ message: { hello: 'world1' } }

示例 2:文件名:index.js

Javascript

// Node.js program to demonstrate the
// MessageChannel.postMessage() method
 
// Importing worker_thread module
 
const { MessageChannel, receiveMessageOnPort }
        = require('worker_threads');
 
// Creating and initializing the MessageChannel
const { port1, port2 } = new MessageChannel();
 
// Catching the event message
port2.on('message', (message) => console.log(message));
 
// Catching the event close
port2.on('close', () => console.log('closed!'));
 
// Sending message to port2
// by using postMessage() method
port1.postMessage('GFG');
 
// Closing port by using
// close() method
port1.close();

输出:

GFG
closed!

使用以下命令运行 index.js 文件:

node index.js

参考: https://nodejs.org/dist/latest-v12.x/docs/api/worker_threads.html#worker_threads_port_postmessage_value_transferlist