简介
子进程消息通信模块。提供子进程(通过 child_process.fork 创建)向主进程发送消息的能力,是框架 IPC 通信机制在子进程端的核心实现。
子进程不能直接访问 Electron 的 ipcRenderer/ipcMain,因此通过 Node.js 内置的 process.send() 方法与主进程通信。主进程通过监听子进程的 message 事件来接收和分发这些消息。
消息格式遵循框架统一 IPC 协议:
{
channel: string, // 通信通道,如 'ee#sendToMain'
eventReceiver: string, // 接收者类型,如 'job' / 'task'
event: string, // 事件名
data: object // 事件数据
}模块同时导出 ChildMessage 类(用于自定义实例化)和 childMessage 单例(用于子进程代码中直接使用)。
导入
CJS 和 ESM:
// ESM
import { ChildMessage, childMessage } from 'ee-core/message';
// CJS
const { ChildMessage, childMessage } = require('ee-core/message');从主入口:
// ESM
import { ChildMessage, childMessage } from 'ee-core';
// CJS
const { ChildMessage, childMessage } = require('ee-core');API
ChildMessage
说明:子进程消息通信类。封装子进程向主进程发送消息的方法,区分不同接收者类型(ChildJob 与 ForkProcess)。此类运行在子进程环境中,依赖 process.send() 进行通信,如果当前进程不支持 process.send(非 fork 进程)则返回 false。可直接实例化用于自定义场景,或使用预创建的 childMessage 单例。
参数:无(类构造函数无参数)
返回值:ChildMessage — 新的子消息通信类实例
示例:
// 创建自定义实例
const myMessenger = new ChildMessage();
myMessenger.sendToMain('customEvent', { data: 42 });
// 或使用单例
childMessage.sendToMain('taskComplete', { result: 42 });参见:childMessage 单例、Receiver 常量
childMessage
说明:预创建的 ChildMessage 单例实例。可在子进程代码中直接使用,无需实例化。所有 ChildMessage 方法均可在此单例上调用。
参数:无(这是预创建的单例实例)
返回值:ChildMessage — 单例子消息实例
示例:
const { childMessage } = require('ee-core/message');
// 发送到主进程的 ChildJob 处理器
childMessage.sendToMain('job-timer-progress', { jobId, number: 0 });
// 发送到主进程的 Fork 进程处理器
childMessage.send('dataReady', { items: [1, 2, 3] });
// 报告错误
childMessage.sendErrorToTerminal(new Error('Task processing failed'));
// 退出子进程
childMessage.exit(0);sendToMain(eventName, params)
说明:向主进程中的 ChildJob 实例发送消息。使用 Receiver.childJob('job')作为接收者类型。主进程收到消息后,将事件路由到对应的 ChildJob 处理器(通过 myjob.on() 监听)。
参数:| 参数 | 类型 | 必填 | 说明 |
| eventName | string | 是 | 事件名,主进程据此分发到对应的 ChildJob 处理器 |
| params | Record<string, unknown> | 否 | 事件参数,默认 {} |
返回值:boolean — 发送成功返回 true,当前进程不支持 process.send(非 fork 进程)时返回 false
示例:
// 在主进程中监听
myjob.on('job-timer-progress', (data) => {
console.log('Progress:', data.number);
});
// 在子进程中发送
const { childMessage } = require('ee-core/message');
childMessage.sendToMain('job-timer-progress', { jobId, number: 5 });参见:Receiver.childJob、ChildJob
send(eventName, params, receiver)
说明:向主进程发送消息(通用方法)。构造符合框架 IPC 协议的消息对象并通过 process.send() 发送。如果不指定接收者,默认使用 Receiver.forkProcess('task'),将消息路由到 ForkProcess 处理器(通过 task.emitter.on() 监听)。
参数:| 参数 | 类型 | 必填 | 说明 |
| eventName | string | 是 | 事件名 |
| params | Record<string, unknown> | 否 | 事件参数,默认 {} |
| receiver | string | 否 | 接收者类型:'job' 为 ChildJob 类型子进程,'task' 为 Fork 进程(通用任务)。默认 'task' |
返回值:boolean — 发送成功返回 true,当前进程不支持 process.send 时返回 false
示例:
// 在主进程中监听(Fork 任务)
const timerTask = myJob.exec('./jobs/example/timer', { jobId });
timerTask.emitter.on('job-timer-progress', (data) => {
console.log('Task progress:', data);
});
// 在子进程中发送(默认接收者:'task')
childMessage.send('job-timer-progress', { jobId, number: 0, pid: 0 });
// 明确发送到 ChildJob 处理器
childMessage.send('customEvent', { result: 42 }, 'job');参见:Receiver.forkProcess、Receiver.childJob、sendToMain
exit(code)
说明:退出当前子进程。使用指定退出码调用 process.exit(),立即终止子进程。此方法不会正常返回。
参数:| 参数 | 类型 | 必填 | 说明 |
| code | number | 否 | 退出码,默认 0(正常退出) |
返回值:never — 永不返回(process.exit 终止进程)
示例:
// 任务完成后正常退出
childMessage.exit(0);
// 以错误码退出
childMessage.exit(1);sendErrorToTerminal(err)
说明:向主进程发送错误信息并在渲染进程中显示异常对话框。通过 Processes.showException('ee#showException')通道发送。主进程收到后通过 IPC 转发到渲染进程,渲染进程显示包含错误信息的对话框。错误消息包含提示用户查看日志文件以获取详细信息。
参数:| 参数 | 类型 | 必填 | 说明 |
| err | Error | 是 | 要转发的错误对象 |
返回值:boolean — 发送成功返回 true,当前进程不支持 process.send 时返回 false
示例:
try {
// 可能失败的任务处理
processData();
} catch (err) {
// 将错误转发到主进程显示
childMessage.sendErrorToTerminal(err);
// 显示的错误消息:"Error: processData failed Error !!! Please See file ee-core.log or ee-error-xxx.log for details !"
childMessage.exit(1);
}参见:Processes.showException
