本文由小千给大家分享Electron之IPC 通信。

1、index.html

<!DOCTYPE html><html><head><meta charset="UTF-8"><meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'"><title>Hello World!</title></head><body><h1>Hello World!</h1><button type="button" id="talk">Talk to main process</button><br><!-- All of the Node.js APIs are available in this renderer process. -->We are using Node.js <strong><script>document.write( process.versions.node)</script></strong>,and Electron <strong><script>document.write( process.versions.electron )</script></strong>.<script>// You can also require other files to run in this processrequire('./renderer.js')</script></body></html>

2、renderer.js

// This file is required by the index.html file and will// be executed in the renderer process for that window.// All of the Node.js APIs are available in this process.const { ipcRenderer } = require('electron')let i = 1setInterval( () => {console.log(i)i++}, 1000)document.getElementById('talk').addEventListener('click', e => {// ipcRenderer.send( 'channel1', 'Hello from main window')let response = ipcRenderer.sendSync( 'sync-message', 'Waiting for response')console.log(response)})ipcRenderer.on( 'channel1-response', (e, args) => {console.log(args)})ipcRenderer.on( 'mailbox', (e, args) => {console.log(args)})

3、main.js

// Modulesconst {app, BrowserWindow, ipcMain} = require('electron')// Keep a global reference of the window object, if you don't, the window will// be closed automatically when the JavaScript object is garbage collected.let mainWindow// Create a new BrowserWindow when `app` is readyfunction createWindow () {mainWindow = new BrowserWindow({width: 1000, height: 800, x: 100, y:140,webPreferences: { nodeIntegration: true }})// Load index.html into the new BrowserWindowmainWindow.loadFile('index.html')// Open DevTools - Remove for PRODUCTION!mainWindow.webContents.openDevTools();mainWindow.webContents.on( 'did-finish-load', e => {// mainWindow.webContents.send( 'mailbox', {//   from: 'Ray',//   email: 'ray@stackacademy.tv',//   priority: 1// })})// Listen for window being closedmainWindow.on('closed',  () => {mainWindow = null})}ipcMain.on( 'sync-message', (e, args) => {console.log(args)setTimeout( () => {e.returnValue = 'A sync response from the main process'}, 4000)})ipcMain.on( 'channel1', (e, args) => {console.log(args)e.sender.send( 'channel1-response', 'Message received on "channel1". Thank you!')})// Electron `app` is readyapp.on('ready', createWindow)// Quit when all windows are closed - (Not macOS - Darwin)app.on('window-all-closed', () => {if (process.platform !== 'darwin') app.quit()})// When app icon is clicked and app is running, (macOS) recreate the BrowserWindowapp.on('activate', () => {if (mainWindow === null) createWindow()})

希望本文的分享能帮到大家!本文来自千锋教育,转载请注明出处。

web前端技术分享Electron之IPC 通信相关推荐

  1. web前端培训分享Electron之IPC 通信

    本文由小千给大家分享Electron之IPC 通信. 1.index.html <!DOCTYPE html><html><head><meta charse ...

  2. web前端技术分享Electron之Renderer Process API

    本文由小千给大家分享Renderer Process API,首先Renderer API 主要包括 remote.Browser window proxy.desktop Capture Rende ...

  3. 好程序员web前端技术分享媒体查询

    为什么80%的码农都做不了架构师?>>>    好程序员web前端技术分享媒体查询 什么是媒体查询 媒体查询可以让我们根据设备显示器的特性(如视口宽度.屏幕比例.设备方向:横向或纵向 ...

  4. Web前端技术分享:学习HTML和CSS的5大理由

    人们学习HTML和CSS最常见的原因是开始从事Web开发.但并不是只有Web开发人员才要学习HTML和CSS的核心技术.作为一个网络用户,你需要你掌握的相关技术很多,但下面小千总结了5个你无法拒绝学习 ...

  5. web前端技术分享:前端开发与后端开发的区别是什么?

    相信很多人在技术岗都听到过前端和后端这两个职位,但是大部分人对前端开发与后端开发的区别是什么?并不是很清楚,下面小千就为大家详细的介绍一下两者的区别之处. web前端分享:前端开发与后端开发的区别是什 ...

  6. web前端培训分享Electron之Main Process API

    本节由千锋web前端培训机构讲师给大家分享Electron使用指南之Main Process API,Electron API (Electron API 有三种) Main Process (主进进 ...

  7. web前端技术分享:web前端的求职前景好不好?

    想要进军互联网行业,大部分人会选择web前端和web前端这两个技术,因为两者之间的发展前景是非常好的.下面我们就先来了解一下web前端的求职前景好不好? web前端的求职前景好不好?近几年的热门行业里 ...

  8. web前端技术分享:详解模块化require 和 import的区别

    在前端开发中,我们可以使用很多模块化的库来帮助我们更好的实现效果,有一些库的功能类似很多同学就不知道该如何选择,比如require和import,今天小千就给大家介绍一下两者的区别. 一.区别 1.r ...

  9. Web前端技术分享:img标签下方出现空隙的问题解决方案

    很多初学小伙伴在写页面的时候碰到过一个问题,当我们用一个div直接包裹img,img的下方会出现3px间距,如图 代码如下: 为什么会出现这种情况呢? 因为img是一种类似text的标签元素,在结束的 ...

最新文章

  1. 单手也能创奇迹!独臂博士单手敲代码获奖 30 余项:感恩所有的善意
  2. Centos7 安装 memcached 1.4.25
  3. python一个图画两条曲线_用python建立两个Y轴的XY曲线图方法
  4. VBS脚本常用经典代码收集
  5. 云来重置任意用户密码
  6. 五、【SAP-PM模块】服务采购业务流程
  7. matlab 多重循环在最外层加断点_循环优化之循环分块(loop tiling)
  8. python编程一球从100米_Python练习题 015:一颗自由落地的球
  9. Python基础入门知识实例【基础算法】
  10. 什么是车联网?导航?听歌?智能语音识别?事实没这么简单!
  11. JAVA 16进制转字符串问题
  12. Linux 命令大全
  13. layui后台添加数据给php,layui怎么实现数据绑定
  14. 深信服2023秋季校园招聘C++笔试A卷
  15. 无法从命令行或调试器启动服务,必须首先安装Windows服务(使用installutil.exe),然后用ServerExplorer、Windows服务器管理工具或NET START命令启动它
  16. 哥德巴赫猜想:任意大于6的偶数都可以被分解成两个素数之和
  17. node.js卸载与重装
  18. Sea.js框架构建
  19. 掌握这些电脑知识,你可以玩的很无耻(暴强)
  20. SOLID 设计原则 (有点长但很透彻)

热门文章

  1. 10个快速提升技术水平的方法
  2. 使用onnx包将pth文件转换为onnx文件
  3. Codeforces 1196D RGB Substring
  4. SSY and JLBD
  5. Vitaly and Night
  6. tmux命令启动MySQL_tmux启动脚本
  7. php cli spl_autoload_register,PHP spl_autoload_register 自动加载方法浅析
  8. 零宽断言java_正则表达之零宽断言(零宽度正预测先行断言)
  9. ubuntu18.04.4 LTS 安装NVIDIA驱动亲测有用方法2020年最新及常见问题避坑
  10. 帮你梳理springboot所有常用注解