当前位置:网站首页>electron 基础项目搭建 &&主线程和渲染线程的通信
electron 基础项目搭建 &&主线程和渲染线程的通信
2022-06-25 18:27:00 【霹雳桃】
electron
一、初始化一个 electron 项目
1.先创建一个 package.json 文件
npm init -y
2.下载安装 electron
- 注意要切换淘宝镜像下载
npm instsll electron -S
3.在 package.json 下配置执行脚本
- 先安装
nodemon
"scripts": {
"start": "nodemon --exec electron . --watch ./ --ext .html,.js,.css"
},
4.在根目录下创建 main.js 文件
const {
app, BrowserWindow } = require("electron");
// 主进程
const createWindow = () => {
const win = new BrowserWindow({
// 窗口大小
width: 1400,
height: 800,
// 最小的窗口大小
minHeight: 300,
minWidth: 400,
// 窗口是否显示
show: true,
// 是否可以拖动
movable: true,
// 是否有顶部状态栏,拖动条
// frame: false,
// 导致隐藏的标题栏和完整大小的内容窗口
// titleBarStyle: "default",
// 窗口初始背景颜色
backgroundColor: "aliceblue",
});
// 打开调试工具
win.webContents.openDevTools();
// 给窗口转载页面
win.loadFile("./render/index.html");
win.once("ready-to-show", () => {
win.show();
});
};
app.on("window-all-closed", () => {
console.log("window-all-closed");
// 对于 MACOS 系统,关闭窗口时,不会直接推出应用
if (process.platform !== "darwin") {
app.quit();
}
});
app.whenReady().then(() => {
createWindow();
// 在 MacOS 下,当全部窗口关闭,点击 dock 图标,窗口再次打开。
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// console.log(app.isReady())
// console.log(app.getPath('desktop'))
// console.log(app.getPath('music'))
// console.log(app.getPath('temp'))
// console.log(app.getPath('userData'))
// console.log(BrowserWindow.getAllWindows().length)
});
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
5.在根目录下创建文件夹 render 里面创建入口文件 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- <meta http-equiv="Content-Security-Policy" content="script-src 'self' unsafe-eval"> -->
<title>Document</title>
</head>
<body>
<h1>第一个 electron 项目</h1>
</body>
</html>
二、主进程和渲染进程通信
- 目录结构
--根目录
--controller 封装的主线程收发数据
--ipcMessage.js
--preload contextBridge 方法,主线程和渲染线程沟通的桥梁
--index.js
--render 加载的页面入口
--app.js
--index.html
--style.css
--vue.global.js vue的项目包,本示例页面使用 vue3
--main.js 项目入口
1.通信之前需要有 contextBridge 作为主线程和渲染现场之间沟通的桥梁
- 使用
contextBridge方法,将主线程的信息发送到渲染线程
// 例,将 electron 和 node 版本号发往渲染现线程
const {
contextBridge } = require("electron");
// 发送 ,该信息的 key 为 myAPI
contextBridge.exposeInMainWorld("myAPI", {
versions: process.versions,
});
// 页面中获取方法
const versions = window.myAPI.versions;
// 获取版本号
// chromeVersion: versions.chrome,
// NodeVersion: versions.node,
// electronVersion: versions.electron
2.渲染进程向主进程发送同步信息
- 渲染进程使用方法
ipcRenderer.send('事件名','信息')发送消息,此操作在contextBridge中 - 主线程使用方法
ipcMain接收,使用方法event.sender.send返回内容
// --------------contextBridge------------------
// 创建方法 sendSync 发送消息
const sendSync = (message) => {
ipcRenderer.send("sync-send-event", message);
};
// 讲方法抛出到 渲染线程调用
contextBridge.exposeInMainWorld("myAPI", {
sendSync,
});
// -----------------渲染线程--------------------------
// 渲染线程调用
myAPI.sendSync("from renderer message 1");
// -----------------------主线程--------------------
// 主线程接受数据
// 同步事件监听
ipcMain.on("sync-send-event", (event, msg) => {
// console.log(msg);
// 使用参数 event 的方法 返回消息
event.sender.send("sync-receive-event", "我已经收到:" + msg);
});
contextBridge接收消息,并将方法抛出到渲染线程,渲染线程创建事件监听
// -----------------contextBridge-------------------
// 使用 ipcRenderer.on 创建时间同步监听
const recieveSyncMsg = () => {
return new Promise((resolve) => {
ipcRenderer.on("sync-receive-event", (event, msg) => {
// console.log(msg)
resolve(msg);
});
});
};
// 将事件 recieveSyncMsg 发送到渲染线程
contextBridge.exposeInMainWorld("myAPI", {
recieveSyncMsg,
});
// --------------------渲染线程-------------------
// 使用async await 创建同步事件监听
async mounted(){
const result = await myAPI.recieveSyncMsg();
}
以上是一套完整的渲染线程发送数据 -> 主线程接受数据并返回 -> 渲染线程接收返回数据 的过程
3.渲染进程向主进程发送异步消息
- 渲染线程发送数据使用
ipcRenderer.invoke方法,发送数据的方法在contextBridge中使用,同样将方法抛出到渲染线程的 js 文件调用 - 主线程接受数据使用方法
ipcMain.handle
// --------------contextBridge-------------------
// 发送请求的方法 ipcRenderer.invoke 的第一个参数是本次请求的 key,第二个参数可有可无, 使用 return 给渲染线程返回数据
const sendAsync = async () => {
const result = await ipcRenderer.invoke("my-invokable-ipc");
return result;
};
// 讲方法 抛出到渲染线程的页面
contextBridge.exposeInMainWorld("myAPI", {
sendAsync,
});
// ----------------渲染线程---------------------------
// 如果有桥接的方法(contextBridge),建议IPC通信,全部应用异步。
// 点击事件的方法
async sendAsyncMsg() {
const result = await myAPI.sendAsync()
console.log(result)
}
// --------------主线程就收数据和返回数据--------------------
// 异步事件监听
// 如果渲染线程有数据传来可以使用 args 接收
ipcMain.handle('my-invokable-ipc', async (event, ...args) => {
const result = await somePromise()
return result
})
// 写一个异步函数,模拟异步方法,三秒后返回数据
function somePromise() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('message from main process.')
}, 3000)
})
}
边栏推荐
- Training of long and difficult sentences in postgraduate entrance examination day89
- Hash of redis command
- 正则表达式总结
- anaconda下载清华源
- Training of long and difficult sentences in postgraduate entrance examination day88
- RMAN backup database_ Use RMAN for split mirror backup
- [deeply understand tcapulusdb technology] tmonitor system upgrade
- Analysis on China's aluminum foil output, trade and enterprise leading operation in 2021: dongyangguang aluminum foil output is stable [figure]
- 【深入理解TcaplusDB技术】TcaplusDB运维单据
- Training of long and difficult sentences in postgraduate entrance examination day81
猜你喜欢
![[deeply understand tcapulusdb technology] tcapulusdb import data](/img/31/4e33fafa090e0bb5b55e11978cdff8.png)
[deeply understand tcapulusdb technology] tcapulusdb import data

158_模型_Power BI 使用 DAX + SVG 打通制作商業圖錶幾乎所有可能

Leetcode force buckle (Sword finger offer 26-30) 26 Substructure of tree 27 Image of binary tree 28 Symmetric binary tree 29 Print matrix 30 clockwise Stack containing min function
![Development status of China's hydrotalcite industry in 2020 and analysis of major enterprises: the market scale is rapidly increasing, and there is a large space for domestic substitution [figure]](/img/2c/05f2aa467edb76095e30a117adc251.jpg)
Development status of China's hydrotalcite industry in 2020 and analysis of major enterprises: the market scale is rapidly increasing, and there is a large space for domestic substitution [figure]
![Overview and trend analysis of China's CT examination equipment industry in 2021 [figure]](/img/e0/d4ed9a9d91534be0d956414f6abaaa.jpg)
Overview and trend analysis of China's CT examination equipment industry in 2021 [figure]

SVN介绍及使用总结
![[deeply understand tcapulusdb technology] create a game area for document acceptance](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[deeply understand tcapulusdb technology] create a game area for document acceptance
![[elt.zip] openharmony paper Club - witness file compression system erofs](/img/db/2f1c85ebd4ce9b84d9fd5406a8de4b.png)
[elt.zip] openharmony paper Club - witness file compression system erofs

Basic operation details of binary search tree (BST) (complete code, including test cases)

【深入理解TcaplusDB技術】TcaplusDB業務數據備份
随机推荐
Boiled peanuts
158 Bar _ Modèle Power bi utilise Dax + SVG pour créer des diagrammes d'affaires presque toutes les possibilités
Analysis on market scale and supply of China's needle coke industry in 2020 [figure]
【深入理解TcaplusDB技術】TcaplusDB業務數據備份
RMAN backup database_ Use RMAN for split mirror backup
[in depth understanding of tcapulusdb technology] business guide for creating doc acceptance
Analysis on policy, output and market scale of China's natural gas hydrogen production industry in 2020 [figure]
Training of long and difficult sentences in postgraduate entrance examination day85
One article solves all search backtracking problems of Jianzhi offer
06 local method interface
正则表达式总结
PHP数据库连接version1.1
Training of long and difficult sentences in postgraduate entrance examination day81
RMAN backup database_ catalogue
LeetCode力扣(剑指offer 26-30)26. 树的子结构 27. 二叉树的镜像 28. 对称的二叉树 29. 顺时针打印矩阵 30. 包含min函数的栈
Regular expression summary
RMAN备份数据库_跳过脱机,只读和不可访问的文件
Some recursive and iterative problem solving ideas of binary tree (clear and easy to understand)
Overview and trend analysis of China's foreign direct investment industry in 2020 [figure]
158_模型_Power BI 使用 DAX + SVG 打通制作商业图表几乎所有可能