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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。