MessageChannel.close()方法是worker_threads模块内的Worker类的内置应用程序编程接口,用于禁用消息端口对象以发送其他消息。
用法:
const MessageChannel.close()
参数:此方法不接受任何参数。
返回值:此方法不返回任何值。
范例1: 文件名:index.js
// Node.js program to demonstrate the
// MessageChannel.close() method
// Importing worker_thread module
const { MessageChannel, receiveMessageOnPort }
= require('worker_threads');
// Creating and intializng the MessageChannel
const { port1, port2} = new MessageChannel();
// Posting data in port1
port1.postMessage({ hello:'world1' });
// Posting data in port2
port2.postMessage({ hello:'world2' });
/// Display the result
console.log("recived data in port1:");
console.log( receiveMessageOnPort(port1));
console.log("recived data in port2:");
console.log( receiveMessageOnPort(port2));
// Closing the ports
port1.close();
port2.close();
使用以下命令运行index.js文件:
node index.js
输出:
recived data in port1: { message:{ hello:'world2' } } recived data in port2: { message:{ hello:'world1' } }
范例2: 文件名:index.js
// Node.js program to demonstrate the
// MessageChannel.close() Method
// Importing worker_thread module
const { MessageChannel, receiveMessageOnPort }
= require('worker_threads');
// Creating and intializng the MessageChannel
const { port1, port2} = new MessageChannel();
// Cathing the event message
port2.on('message', (message) => console.log(message));
// Cathing the event close
port2.on('close', () => console.log('closed!'));
// Sending message to port2
port1.postMessage('GFG');
// Closing port by using close() method
port1.close();
使用以下命令运行index.js文件:
node index.js
输出:
GFG closed!
参考: https://nodejs.org/dist/latest-v12.x/docs/api/worker_threads.html#worker_threads_port_close
相关用法
- Node.js console.timeLog()用法及代码示例
- Node.js GM implode()用法及代码示例
- Node.js GM drawPolygon()用法及代码示例
- Node.js GM sharpen()用法及代码示例
- Node.js GM edge()用法及代码示例
- Node.js GM write()用法及代码示例
- Node.js GM channel()用法及代码示例
- Node.js GM roll()用法及代码示例
- Node.js GM whiteThreshold()用法及代码示例
- Node.js GM whitePoint()用法及代码示例
- Node.js GM drawEllipse()用法及代码示例
- Node.js GM threshold()用法及代码示例
- Node.js GM chop()用法及代码示例
- Node.js GM thumbnail()用法及代码示例
- Node.js GM paint()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node.js MessageChannel.close() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。