在本文中,我们将讨论 Process 对象的断开连接事件。如果 Node.js 进程是使用 IPC 通道启动的,则当 IPC 通道关闭时,将发出断开连接事件。
用法:
process.on('disconnect', () => { // Disconnect event });
下面的示例说明了 Node.js 中进程断开事件属性的用法:
示例 1:
文件名:parent.js
Javascript
// Require fork method from child_process
// to spawn child process
const fork = require('child_process').fork;
// Child process file
const child_file = 'child.js';
// Spawn child process
const child = fork(child_file);
文件名:child.js
Javascript
console.log('In child.js')
if (process.connected) {
console.log("Child Process connected!")
console.log("Disconnecting in 3000ms")
setTimeout(() => {
process.disconnect();
}, 3000);
}
process.on('disconnect', () => {
console.log("Child Process disconnected!")
});
输出:
In child.js Child Process connected! Disconnecting in 3000ms Child Process disconnected!
示例 2:
文件名:parent.js
Javascript
// Require fork method from child_process
// to spawn child process
const fork = require('child_process').fork;
// Child process file
const child_file = 'child2.js';
// Spawn child process
const child = fork(child_file);
文件名:child2.js
Javascript
console.log("In Child Process!")
process.disconnect()
process.on('disconnect', () => {
console.log(`Child process disconnected`);
});
输出:
In Child Process! Child process disconnected
参考:https://nodejs.org/api/process.html#event-disconnect
相关用法
- Node.js Process beforeExit用法及代码示例
- Node.js Process warning用法及代码示例
- Node.js Process uncaughtException用法及代码示例
- Node.js Process multipleResolves用法及代码示例
- Node.js Process exit用法及代码示例
- Node.js Process unhandledPromiseRejection用法及代码示例
- Node.js Process message用法及代码示例
- Node.js Process unhandledRejection用法及代码示例
- Node.js PNG转JPG用法及代码示例
- Node.js PerformanceObserverEntryList.getEntries()用法及代码示例
- Node.js PerformanceObserverEntryList.getEntriesByName(name[, type])用法及代码示例
- Node.js PerformanceObserverEntryList.getEntriesByType(type)用法及代码示例
- Node.js urlObject.auth()用法及代码示例
- Node.js process.env()用法及代码示例
- Node.js process.argv0()用法及代码示例
- Node.js process.argv()用法及代码示例
- Node.js process.arch()用法及代码示例
- Node.js Decipher.final()用法及代码示例
- Node.js crypto.createDiffieHellman()用法及代码示例
- Node.js v8.deserializer.readHeader()用法及代码示例
- Node.js v8.deserializer.readRawBytes()用法及代码示例
- Node.js v8.deserializer.readUint32()用法及代码示例
- Node.js v8.deserializer.readUint64()用法及代码示例
- Node.js v8.deserializer.readValue()用法及代码示例
- Node.js v8.serializer.releaseBuffer()用法及代码示例
注:本文由纯净天空筛选整理自aritrikghosh784大神的英文原创作品 Node.js Process disconnect Event。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。