【问题标题】:How to differentiate two ipc renderer in one ipc main (same channel)如何区分一个 ipc 主(同一通道)中的两个 ipc 渲染器
【发布时间】:2019-10-11 03:29:23
【问题描述】:

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

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

【问题讨论】:

  • 您想知道发送postMessage事件的浏览器窗口吗?
  • 是的。目前我有两个 browserWindow 连接到具有相同事件的主进程

标签: electron ipc ipcmain


【解决方案1】:

您可以通过访问作为第一个参数的 events 对象中的 sender 对象,从主进程获取当前的 webcontent id。

   console.log(event.sender.webContents.id);

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

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

当主进程收到这个事件时,你只需要访问消息对象中的winId属性

【讨论】:

    【解决方案2】:

    您可以在 ipc message payoad 中传递身份,也可以通过 ipc message 的 sender 对象获取 windows web 内容 id。

    【讨论】:

    • 感谢您的回答。我做了第一个选择,但我正在寻找替代方案。你能告诉我如何获得窗口内容ID吗?我尝试了几种方法来获取 id,但没有成功。
    • 您可以获取在 ipc.on 回调中可用的 event.sender 对象。 Event.sender.senderId 将包含发送者的窗口内容id
    猜你喜欢
    • 2021-05-13
    • 2018-12-05
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    相关资源
    最近更新 更多