zoukankan      html  css  js  c++  java
  • electron熟悉主进程和渲染进程通信 ipcRenderer.send() and ipcMain.to()

    在昨天的时候,已经用过ipcRendered.sendSync(), 昨天的代码是这样的

    renderer.js文件

    const ele_sendbtn = document.getElementById("send_btn")
    ele_sendbtn.onclick = function () {
     console.log("this is renderer output log",ipcRenderer.sendSync('synchronous-message', datas)) //同步处理
        alert(datas)
    }
    main.js 文件中
     
    ipcMain.on('synchronous-message', function (event, arg) {
      console.log(arg);  
     
      event.returnValue = "XXX";
    });
     
    主进程和渲染进程通信成功,如下图

    今天又写了一个是这样的(搞了好久才搞明白,同步异步 处理是不一样的,这是异步处理)

    以下代码是已更正后的结果

     renderer.js 如下:

    submit.onclick = function (e) {

        let data = ele_filepath.files[0].path;
        console.log(data);
        ipcRenderer.send('uploadFile', data)

    }
     main.js 如下:
    ipcMain.on('uploadFile', (event, arg) => {
     
      console.log("filePath:", arg);
      // event.returnValue = {msg:'OK',
      // code:0}
      event.sender.send('uploadFileSuccess',{ 
          msg:'OK',
          code:0
        })
    })

     官网API链接: https://www.electronjs.org/docs/api/ipc-main#ipcmainhandleoncechannel-listener

    划重点:

    • 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.

    PS: 如果能耐心的看完这段话,我就不会被坑了小1天的时间,教训呀

  • 相关阅读:
    【翻译】让你的网站飞起来
    理解ASP.NET MVC中的模型绑定
    【转载】创建定制ASP.NET MVC视图引擎
    修改STM32主频
    Cortex系列ARM核心及体系结构介绍
    递归
    NFD模拟兴趣包的转发
    NX 笔记
    MicroPython 8266 配置
    Python JSON操作
  • 原文地址:https://www.cnblogs.com/fool-jingqiu/p/13368020.html
Copyright © 2011-2022 走看看