当前位置: 首页 > news >正文

electron autoUpdater自动更新使用示例 客户端+服务端

封装好的 update.js 模块 

'use strict';
const { autoUpdater } = require('electron')
// 更新检测
// https://www.electronjs.org/zh/docs/latest/api/auto-updaterconst checkUpdate = (serverUrl) =>{const updateUrl = `${serverUrl}/update?platform=${process.platform}&version=${app.getVersion()}`// 注意这里必须使用trycatch包裹 否则代码无法运行try {autoUpdater.setFeedURL({ updateUrl })// 每隔 5 分钟检查一次更新setInterval(() => {autoUpdater.checkForUpdates()}, 60000 * 5)/*** 更新下载完成的时候触发。* API文档中的返回,即下面的参数,根据返回的参数顺序接收** 返回:event EventreleaseNotes stringreleaseName stringreleaseDate DateupdateURL string*/autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName,releaseDate, updateURL) => {const dialogOpts = {type: 'info',buttons: ['Restart', 'Later'],title: 'Application Update',message: process.platform === 'win32' ? releaseNotes : releaseName,detail:'A new version has been downloaded. Starta om applikationen för att verkställa uppdateringarna.'}dialog.showMessageBox(dialogOpts).then((returnValue) => {if (returnValue.response === 0) autoUpdater.quitAndInstall()})})autoUpdater.on('checking-for-update',(evt)=>{console.log(evt);})autoUpdater.on('update-available',(evt)=>{console.log(evt);})autoUpdater.on('update-not-available',(evt)=>{console.log(evt);})// 错误处理autoUpdater.on('error', (err) => {console.error('There was a problem updating the application')console.error(err)})} catch (error) {console.log(error);}
}module.exports = checkUpdate

main.js中调用

const { app, BrowserWindow, autoUpdater } = require('electron')
const path = require('node:path')const checkUpdate = require('./update')// API服务BASE_URL
const SERVER_URL = 'https://api.tekin.cn/electronAppDemo'const createWindow = () =>{const win = new BrowserWindow({width:800, height: 600,// 预加载webPreferences:{// nodeIntegration: true,sandbox: false,// __dirname 字符串指向当前正在执行的脚本的路径(在本例中,它指向你的项目的根文件夹)。// path.join API 将多个路径联结在一起,创建一个跨平台的路径字符串。preload: path.join(__dirname, 'preload.js')}})// 加载URLwin.loadURL('https://dev.tekin.cn')}// 使用whenReady函数监听
app.whenReady().then(()=>{createWindow()app.on('activate',()=>{//如果没有窗口打开则打开一个窗口 (macOS)if (BrowserWindow.getAllWindows().length===0) {createWindow()}})// 执行更新检测checkUpdate(SERVER_URL)
})// 关闭所有窗口时退出应用 (Windows & Linux)
app.on('window-all-closed',()=>{if (process.platform != 'darwin' ) {app.quit();}
})

服务端API

服务端更新API JSON格式  Update Server JSON Format

仅 url 是必须提供的参数,其他可选

{"url": "https://mycompany.example.com/myapp/releases/myrelease","name": "My Release Name","notes": "Theses are some release notes innit","pub_date": "2013-09-18T12:29:53+01:00"
}

## Update File JSON Format

The alternate update technique uses a plain JSON file meaning you can store your update metadata on S3 or another static file store. The format of this file is detailed below:

```json
{
    "currentRelease": "1.2.3",
    "releases": [
        {
            "version": "1.2.1",
            "updateTo": {
                "version": "1.2.1",
                "pub_date": "2013-09-18T12:29:53+01:00",
                "notes": "Theses are some release notes innit",
                "name": "1.2.1",
                "url": "https://mycompany.example.com/myapp/releases/myrelease"
            }
        },
        {
            "version": "1.2.3",
            "updateTo": {
                "version": "1.2.3",
                "pub_date": "2014-09-18T12:29:53+01:00",
                "notes": "Theses are some more release notes innit",
                "name": "1.2.3",
                "url": "https://mycompany.example.com/myapp/releases/myrelease3"
            }
        }
    ]
}
```

PHP代码示例

     public function update() {// electron autoUpdater  Tekin$data = ["url" => "https://dev.tekin.cn/download/myapp.zip", 'name' => "MyApp 1.0.0", "notes" => "Theses are some release notes innit", "pub_date" => "2013-09-18T12:29:53+01:00"];exit(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));}

相关文章:

  • Debian Linux完全卸载gitlab-ce
  • Servlet见解2
  • Typora Mac激活
  • 2024 年甘肃省职业院校技能大赛 应用软件系统开发赛项样题
  • Elasticsearch可视化平台Kibana [ES系列] - 第498篇
  • html页面 通过jquery.i18n.properties添加多语言
  • web网页端使用webSocket实现语音通话功能(SpringBoot+VUE)
  • CMMI-项目总体计划模版
  • 【Jmeter、postman、python 三大主流技术如何操作数据库?】
  • 前端---css 选择器
  • iPad绘画之旅:从小白到文创手账设计的萌系简笔画探索
  • 基于双闭环PI的SMO无速度控制系统simulink建模与仿真
  • 华为gre隧道全部跑静态路由
  • Vue 初始化數組后操作另一個數組onMounted和watch
  • 单体项目-动态上下文问题
  • 【EOS】Cleos基础
  • Android组件 - 收藏集 - 掘金
  • avalon2.2的VM生成过程
  • Github访问慢解决办法
  • Git同步原始仓库到Fork仓库中
  • HomeBrew常规使用教程
  • JavaScript异步流程控制的前世今生
  • java取消线程实例
  • JAVA之继承和多态
  • PHP 使用 Swoole - TaskWorker 实现异步操作 Mysql
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • Web设计流程优化:网页效果图设计新思路
  • windows下mongoDB的环境配置
  • 初识 webpack
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 数组大概知多少
  • 新书推荐|Windows黑客编程技术详解
  • 优化 Vue 项目编译文件大小
  • ​LeetCode解法汇总518. 零钱兑换 II
  • ​你们这样子,耽误我的工作进度怎么办?
  • # 计算机视觉入门
  • ## 临床数据 两两比较 加显著性boxplot加显著性
  • #php的pecl工具#
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • (3)nginx 配置(nginx.conf)
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (第二周)效能测试
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (附源码)ssm高校实验室 毕业设计 800008
  • (附源码)ssm基于微信小程序的疫苗管理系统 毕业设计 092354
  • (企业 / 公司项目)前端使用pingyin-pro将汉字转成拼音
  • (深入.Net平台的软件系统分层开发).第一章.上机练习.20170424
  • (四)linux文件内容查看
  • (一)Thymeleaf用法——Thymeleaf简介
  • .net framework profiles /.net framework 配置
  • .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?
  • .Net 基于MiniExcel的导入功能接口示例
  • .net 怎么循环得到数组里的值_关于js数组
  • .net实现客户区延伸至至非客户区
  • .sh文件怎么运行_创建优化的Go镜像文件以及踩过的坑