当前位置: 首页 > 文档资料 > Electron 中文文档 >

ipcMain

优质
小牛编辑
160浏览
2023-12-01

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

线程:主线程

ipcMain模块是EventEmitter类的一个实例。 当在主进程中使用时,它处理从渲染器进程(网页)发送出来的异步和同步信息。 从渲染器进程发送的消息将被发送到该模块。

Communicate asynchronously from the main process to renderer processes.

Process: Main

The ipcMain module is an instance of the EventEmitter class. When used in the main process, it handles asynchronous and synchronous messages sent from a renderer process (web page). Messages sent from a renderer will be emitted to this module.

发送消息

It is also possible to send messages from the main process to the renderer process, see webContents.send for more information.

  • 发送消息时,事件名称为channel
  • 回复同步信息时,需要设置event.returnValue
  • To send an asynchronous message back to the sender, you can use event.reply(...). This helper method will automatically handle messages coming from frames that aren't the main frame (e.g. iframes) whereas event.sender.send(...) will always send to the main frame.

下面是在渲染和主进程之间发送和处理消息的一个例子:

// 在主进程中.
const { ipcMain } = require('electron')
ipcMain.on('asynchronous-message', (event, arg) => {
  console.log(arg) // prints "ping"
  event.reply('asynchronous-reply', 'pong')
})
ipcMain.on('synchronous-message', (event, arg) => {
  console.log(arg) // prints "ping"
  event.returnValue = 'pong'
})
//在渲染器进程 (网页) 中。
const { ipcRenderer } = require('electron')
console.log(ipcRenderer.sendSync('synchronous-message', 'ping')) // prints "pong"
ipcRenderer.on('asynchronous-reply', (event, arg) => {
  console.log(arg) // prints "pong"
})
ipcRenderer.send('asynchronous-message', 'ping')

Sending Messages

It is also possible to send messages from the main process to the renderer process, see webContents.send for more information.

  • When sending a message, the event name is the channel.
  • To reply to a synchronous message, you need to set event.returnValue.
  • To send an asynchronous message back to the sender, you can use event.reply(...). This helper method will automatically handle messages coming from frames that aren't the main frame (e.g. iframes) whereas event.sender.send(...) will always send to the main frame.

An example of sending and handling messages between the render and main processes:

// In main process.
const { ipcMain } = require('electron')
ipcMain.on('asynchronous-message', (event, arg) => {
  console.log(arg) // prints "ping"
  event.reply('asynchronous-reply', 'pong')
})
ipcMain.on('synchronous-message', (event, arg) => {
  console.log(arg) // prints "ping"
  event.returnValue = 'pong'
})
// In renderer process (web page).
const { ipcRenderer } = require('electron')
console.log(ipcRenderer.sendSync('synchronous-message', 'ping')) // prints "pong"
ipcRenderer.on('asynchronous-reply', (event, arg) => {
  console.log(arg) // prints "pong"
})
ipcRenderer.send('asynchronous-message', 'ping')

方法

IpcMain模块有以下方法来侦听事件:

Methods

The ipcMain module has the following method to listen for events:

ipcMain.on(channel, listener)

  • channel String
  • listener Function - 回调函数

    • event IpcMainEvent
    • ...args any[]

监听 channel,当接收到新的消息时 listener 会以 listener(event, args...) 的形式被调用。

ipcMain.on(channel, listener)

  • channel String
  • listener Function

    • event IpcMainEvent
    • ...args any[]

Listens to channel, when a new message arrives listener would be called with listener(event, args...).

ipcMain.once(channel, listener)

  • channel String
  • listener Function

    • event IpcMainEvent
    • ...args any[]

添加一次性的 listener。当且仅当下一个消息发送到 channellistener 才会被调用,随后 <0>listener</0> 会被移除。

ipcMain.once(channel, listener)

  • channel String
  • listener Function

    • event IpcMainEvent
    • ...args any[]

Adds a one time listener function for the event. This listener is invoked only the next time a message is sent to channel, after which it is removed.

ipcMain.removeListener(channel, listener)

  • channel String
  • listener Function

从监听器数组中移除监听 channel 的指定 listener

ipcMain.removeListener(channel, listener)

  • channel String
  • listener Function

Removes the specified listener from the listener array for the specified channel.

ipcMain.removeAllListeners([channel])

  • channel String

删除所有监听者,或特指的 channel 的所有监听者.

ipcMain.removeAllListeners([channel])

  • channel String

Removes listeners of the specified channel.

事件对象

The documentation for the event object passed to the callback can be found in the ipc-main-event structure docs.

Event object

The documentation for the event object passed to the callback can be found in the ipc-main-event structure docs.