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