Electron中文手册
  • 文档概述
  • 开发指南目录
  • 构建说明(Linux)
  • 构建说明(macOS)
  • 构建说明(Windows)
  • 构建系统概述
  • 编码规范
  • 在 macOS 中调试
  • 在 Windows 中调试
  • 在C ++代码中使用clang-format
  • 在调试器中设置符号服务器
  • 源代码目录结构
  • Chrome升级清单
  • Chromium 开发指南
  • V8 开发指南
  • 教程目录
    • 关于 Electron
    • Electron 版本说明
    • API弃用说明
    • 快速入门
    • 支持的平台
    • 桌面环境集成
    • 系统通知
    • 离屏渲染
    • 在线/离线事件检测
    • 多线程
    • REPL
    • 键盘快捷键
    • DevTools扩展
    • 使用原生模块
    • 使用 Pepper Flash 插件
    • 使用 Selenium 和 WebDriver
    • 使用 Widevine CDM 插件
    • 应用分发
    • 应用打包
    • 主进程调试
    • 使用 node-inspector 进行主进程调试
    • 使用 VSCode 进行主进程调试
    • Mac App Store应用提交指南
    • Windows App Store应用提交指南
    • 安全,本地功能和你的责任
    • Headless CI Systems 测试
  • API接口目录
    • API接口之公用接口
      • 应用语言
      • 开发概要
      • 专业术语
      • 常见问题
      • 环境变量
      • 快捷键字符串
      • 命令行
      • 客户端请求
      • 剪贴板
      • <File> H5 File文件操作
      • 无框窗口
      • <window.open> window.open打开新窗口或打开时传递消息
      • 沙盒选项
    • API接口之主进程接口
      • 整体控制
      • 全局快捷键
      • 图标创建与应用
      • 屏幕
      • 窗口
      • 菜单
      • 菜单项
      • 系统托盘
      • 网页内容
      • 从主进程到渲染进程的异步通信
      • 对话框
      • 创建和控制视图
      • 会话
      • 会话,缓存和代理等控制
      • 页面请求
      • HTTP/HTTPS请求处理
      • 协议的注册和处理
      • 使用系统默认应用程序管理文件或URL
      • 下载项管理
      • 进程控制
      • Chromium原生网络库
      • 获取系统首选项
      • 电源状态
      • 节能管理
      • 调试工具
      • 奔溃报告
      • 性能数据收集
      • 自动更新
      • TouchBar触摸条
      • TouchBar触摸条按钮
      • TouchBar触摸条拾色器
      • TouchBar触摸条分组
      • TouchBar触摸条scrubber
      • TouchBar触摸条分段控件
      • TouchBar触摸条label标签
      • TouchBar触摸条弹出框
      • TouchBar触摸条滑块
      • TouchBar触摸条间隔符
    • API接口之渲染进程接口
      • 页面渲染
      • <webview> webview标签
      • 渲染进程与主进程通信
      • 从渲染进程到主进程的异步通信
      • 子窗口
      • 捕获桌面资源
  • 结构列表
    • 蓝牙设备对象
    • 证书对象
    • 证书主体对象
    • Cookie对象
    • 崩溃报告对象
    • 桌面捕获源对象
    • 显示器对象
    • 打印机信息对象
    • 文件过滤器对象
    • 最近使用的项目
    • 常用列表项
    • CPU使用率对象即程序占用的CPU资源
    • IO值对象
    • 内存信息对象
    • 进程内存信息对象
    • 内存使用详细信息
    • Mime类型缓冲区
    • 矩形对象
    • 删除客户端证书对象
    • 删除密码对象
    • Scrubber项对象
    • 分段控制对象
    • 快捷方式对象
    • 任务对象
    • 缩略图工具栏按钮对象
    • 上传blob对象
    • 上传数据对象
    • 上传文件系统对象
    • 上传文件对象
    • 上传原始数据对象
Powered by GitBook
On this page
  • 发送消息
  • 监听事件
  • ipcMain.on(channel, listener)
  • ipcMain.once(channel, listener)
  • ipcMain.removeListener(channel, listener)
  • ipcMain.removeAllListeners([channel])
  • 事件对象
  • event.returnValue
  • event.sender

Was this helpful?

  1. API接口目录
  2. API接口之主进程接口

从主进程到渲染进程的异步通信

Previous网页内容Next对话框

Last updated 4 years ago

Was this helpful?

在主进程中处理由渲染进程发起的异步通信.

进程:

ipcMain 模块是类类的一个实例.

浅显的打个比方,渲染进程给主进程挂个号,这里就开始忙活起来.当然,你也可以从主进程中向渲染进程发送消息.

发送消息

如果从主进程向渲染进程发送消息,请查看

  • 发送消息,事件名为 channel.

  • 回应同步消息, 请设置 event.returnValue.

  • 回应异步消息, 请使用 event.sender.send(...).

在主进程和渲染进程之间发送和处理消息的例子:

// 主进程中
const {ipcMain} = require('electron')
ipcMain.on('asynchronous-message', (event, arg) => {
  console.log(arg)  // 输出 `ping`
  event.sender.send('asynchronous-reply', 'pong')
})

ipcMain.on('synchronous-message', (event, arg) => {
  console.log(arg)  // 输出 `ping`
  event.returnValue = 'pong'
})
// 渲染进程中(即网页).
const {ipcRenderer} = require('electron')
console.log(ipcRenderer.sendSync('synchronous-message', 'ping')) // 输出 `pong`

ipcRenderer.on('asynchronous-reply', (event, arg) => {
  console.log(arg) // 输出 `pong`
})
ipcRenderer.send('asynchronous-message', 'ping')

监听事件

ipcMain.on(channel, listener)

用途:监听 channel,并调用 listener(event, args...) 处理新消息

  • channel String

  • listener Function

ipcMain.once(channel, listener)

用途:一次性监听 channel,当调用 listener(event, args...) 处理新消息后删除监听

  • channel String

  • listener Function - 一次性的 listener.

ipcMain.removeListener(channel, listener)

用途:从指定 channel 的监听数组中删除特定的 listener

  • channel String

  • listener Function

ipcMain.removeAllListeners([channel])

用途:删除所有监听,或指定 channel 的所有监听

  • channel String (可选)

事件对象

传递给 callback 的 event 对象有如下方法:

event.returnValue

将此设置成同步消息中返回的值.

event.sender

返回发送消息的 webContents ,你可以调用 event.sender.send 来回复异步消息,详见[webContents.send][web-contents-send].

EventEmitter
主进程
web-contents-send