Understanding the IPC Module
上章我们在为应用添加上下文菜单时候简单使用了进程间通信(inter-process communication,IPC)机制,本章我们将深入探索IPC模块的原理。虽然IPC可能不是最富魅力的API模块,但它无疑是我们构建应用程序的根基。
Getting Started
Electron程序被分为两个独立的部分:主进程和渲染进程,因此我们需要一个系统以供二者间通信,这个系统就是IPC模块。IPC模块运行在两个进程间传递同步(synchronous)或异步(asynchronous)消息。渲染进程和主进程各有一个特定IPC模块:ipcRenderer
和ipcMain
:
Figure 6-1. The IPC API provides a communication bridge between the processes.
首先还是获取一个干净的Electron项目:
git clone https://github.com/electron/electron-quick-start ipc-example
然后进入项目根目录
cd ipc-example
接着安装依赖
npm install
最后初始化Git
git init
接下来我们就通过这个示例项目来深入探究IPC模块。