如何区分一个ipc main(同一通道)中的两个ipc渲染器

Sam*_*gol 4 ipc electron ipcmain

我想检测特定的框架/浏览器窗口。我有一个主进程和两个浏览器窗口,这三个窗口都使用同一通道相互发送消息。在 IPCMain 中我需要检测其中之一。我看到 IPCmain 事件有一个名为frameId 的函数,但是当我使用它时,我得到了未定义。

ipcMain.once("postMessage", (event, message) => {
    if(!activeRequest) return;
    activeRequest.json(message).send();
});
Run Code Online (Sandbox Code Playgroud)

0.s*_*.sh 5

您可以通过访问事件对象(第一个参数)中的发送者对象来从主进程获取当前的 webcontent id。

   console.log(event.sender.webContents.id);
Run Code Online (Sandbox Code Playgroud)

您还可以通过渲染器进程传递事件来自的窗口的 ID。

  // in the renderer process do this
  electron.ipcRenderer.send("new-message", { 
      winId: electron.remote.getCurrentWebContents().id , 
      message: "Hi"
  });
Run Code Online (Sandbox Code Playgroud)

当主进程收到此事件时,您只需访问winId消息对象中的属性

  • 添加到此,目前在 Electron 中,您只需执行“event.sender.id”即可获取 id。 (3认同)