web前端技术分享Electron之IPC 通信
本文由小千给大家分享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 通信相关推荐
- web前端培训分享Electron之IPC 通信
本文由小千给大家分享Electron之IPC 通信. 1.index.html <!DOCTYPE html><html><head><meta charse ...
- web前端技术分享Electron之Renderer Process API
本文由小千给大家分享Renderer Process API,首先Renderer API 主要包括 remote.Browser window proxy.desktop Capture Rende ...
- 好程序员web前端技术分享媒体查询
为什么80%的码农都做不了架构师?>>> 好程序员web前端技术分享媒体查询 什么是媒体查询 媒体查询可以让我们根据设备显示器的特性(如视口宽度.屏幕比例.设备方向:横向或纵向 ...
- Web前端技术分享:学习HTML和CSS的5大理由
人们学习HTML和CSS最常见的原因是开始从事Web开发.但并不是只有Web开发人员才要学习HTML和CSS的核心技术.作为一个网络用户,你需要你掌握的相关技术很多,但下面小千总结了5个你无法拒绝学习 ...
- web前端技术分享:前端开发与后端开发的区别是什么?
相信很多人在技术岗都听到过前端和后端这两个职位,但是大部分人对前端开发与后端开发的区别是什么?并不是很清楚,下面小千就为大家详细的介绍一下两者的区别之处. web前端分享:前端开发与后端开发的区别是什 ...
- web前端培训分享Electron之Main Process API
本节由千锋web前端培训机构讲师给大家分享Electron使用指南之Main Process API,Electron API (Electron API 有三种) Main Process (主进进 ...
- web前端技术分享:web前端的求职前景好不好?
想要进军互联网行业,大部分人会选择web前端和web前端这两个技术,因为两者之间的发展前景是非常好的.下面我们就先来了解一下web前端的求职前景好不好? web前端的求职前景好不好?近几年的热门行业里 ...
- web前端技术分享:详解模块化require 和 import的区别
在前端开发中,我们可以使用很多模块化的库来帮助我们更好的实现效果,有一些库的功能类似很多同学就不知道该如何选择,比如require和import,今天小千就给大家介绍一下两者的区别. 一.区别 1.r ...
- Web前端技术分享:img标签下方出现空隙的问题解决方案
很多初学小伙伴在写页面的时候碰到过一个问题,当我们用一个div直接包裹img,img的下方会出现3px间距,如图 代码如下: 为什么会出现这种情况呢? 因为img是一种类似text的标签元素,在结束的 ...
最新文章
- 单手也能创奇迹!独臂博士单手敲代码获奖 30 余项:感恩所有的善意
- Centos7 安装 memcached 1.4.25
- python一个图画两条曲线_用python建立两个Y轴的XY曲线图方法
- VBS脚本常用经典代码收集
- 云来重置任意用户密码
- 五、【SAP-PM模块】服务采购业务流程
- matlab 多重循环在最外层加断点_循环优化之循环分块(loop tiling)
- python编程一球从100米_Python练习题 015:一颗自由落地的球
- Python基础入门知识实例【基础算法】
- 什么是车联网?导航?听歌?智能语音识别?事实没这么简单!
- JAVA 16进制转字符串问题
- Linux 命令大全
- layui后台添加数据给php,layui怎么实现数据绑定
- 深信服2023秋季校园招聘C++笔试A卷
- 无法从命令行或调试器启动服务,必须首先安装Windows服务(使用installutil.exe),然后用ServerExplorer、Windows服务器管理工具或NET START命令启动它
- 哥德巴赫猜想:任意大于6的偶数都可以被分解成两个素数之和
- node.js卸载与重装
- Sea.js框架构建
- 掌握这些电脑知识,你可以玩的很无耻(暴强)
- SOLID 设计原则 (有点长但很透彻)
热门文章
- 10个快速提升技术水平的方法
- 使用onnx包将pth文件转换为onnx文件
- Codeforces 1196D RGB Substring
- SSY and JLBD
- Vitaly and Night
- tmux命令启动MySQL_tmux启动脚本
- php cli spl_autoload_register,PHP spl_autoload_register 自动加载方法浅析
- 零宽断言java_正则表达之零宽断言(零宽度正预测先行断言)
- ubuntu18.04.4 LTS 安装NVIDIA驱动亲测有用方法2020年最新及常见问题避坑
- 帮你梳理springboot所有常用注解