Mrcdh技术博客 Mrcdh技术博客
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Mrcdh

全栈开发小学生
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Html5

  • JavaScript

  • Nodejs

  • express

  • electron

    • electron窗体操作
    • electron应用更新
      • 搭建一个服务放置安装包
        • win需要的文件
      • 检测更新
        • 第一步:在build中配置publish字段
        • 第二步:在应用程序主进程中调用electron-updater模块检测更新
      • 原理
    • 02.electron打包操作(electron-builder)
    • electron调用打印机打印
    • electron和页面通信
    • electron文件缓存
    • electron常见问题
  • Android

  • 微信公众号

  • 框架

  • 其他

  • selenium

  • Sequelize

  • 大前端
  • electron
mrcdh
2022-06-13
目录

electron应用更新

# 搭建一个服务放置安装包

也就是通过 http://服务地址/latest.ym 能够访问到就行

文件的版本号是根据 package.json 中 version 自动识别的

# win需要的文件

  • latest.yml
  • Demo Setup 1.0.1.exe
  • Demo Setup 1.0.1.exe.blockmap

# 检测更新

yarn add electron-updater
1

# 第一步:在build中配置publish字段

"build": {
  ...
  "publish": [
    {
       "provider": "generic",
       "url": "http://服务地址/" 
    }
  ]
}
1
2
3
4
5
6
7
8
9

# 第二步:在应用程序主进程中调用electron-updater模块检测更新

import {dialog} from 'electron'
const { autoUpdater } = require('electron-updater')
function checkUpdate(){
    // 更新窗口
    let win = null

    //默认会自动下载新版本,如果不想自动下载,设置autoUpdater.autoDownload = false
    autoUpdater.autoDownload = false

    if(process.platform == 'darwin'){  
        // 安装包放在了服务端`darwin`目录下
        autoUpdater.setFeedURL('http://服务地址/darwin')  //设置要检测更新的路径
    }else{
        // 同理
        autoUpdater.setFeedURL('http://服务地址/win32')
    }

    // 检测是否有新版本
    autoUpdater.checkForUpdates()

    autoUpdater.on('checking-for-update', res => {
        log.info("获取版本信息:" + res)
    })

    autoUpdater.on('update-not-available', res => {
        log.info("没有可更新版本:" + res)
    })

    // 监听'error'事件
    autoUpdater.on('error', (err) => {
        console.log(err)
    })

    //监听'update-available'事件,发现有新版本时触发
    autoUpdater.on('update-available', () => {
        dialog.showMessageBox({
            type: 'info',
            title: '软件更新',
            message: '发现新版本, 确定更新?',
            buttons: ['确定', '取消']
        }).then(resp => {
            if (resp.response == 0) {
                createWindow()
                // 下载更新
                autoUpdater.downloadUpdate()
            }
        })
    })

    // 下载进度
    autoUpdater.on('download-progress', res => {
        log.info("下载监听:" + res)
        win.webContents.send('downloadProgress', res)
    })


    //监听'update-downloaded'事件,新版本下载完成时触发
    autoUpdater.on('update-downloaded', () => {
        dialog.showMessageBox({
            type: 'info',
            title: '应用更新',
            message: '发现新版本,是否更新?',
            buttons: ['是', '否']
        }).then((buttonIndex) => {
            if(buttonIndex.response == 0) {  //选择是,则退出程序,安装新版本
                autoUpdater.quitAndInstall() 
                app.quit()
            }
        })
    })

    // 下载完成
    autoUpdater.on('update-downloaded', () => {
        dialog.showMessageBox({
            title: '下载完成',
            message: '最新版本已下载完成, 退出程序进行安装'
        }).then(() => {
            // 退出并安装
            autoUpdater.quitAndInstall()
        })
    })

    // 创建更新窗口
    async function createWindow() {
        win = new BrowserWindow({
            width: 300,
            height: 300,
            title: "更新窗口",
            frame: false, 
            transparent: true,
            maximizable: false,
            webPreferences: {
                nodeIntegration: true,
                contextIsolation: false,
                enableRemoteModule: true
            },
        })
        if (process.env.WEBPACK_DEV_SERVER_URL) {
            await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL + '#/update')
        } else {
            win.loadURL('app://./index.html#/update')
        }
    }
}

app.on('ready', () => {
    //每次启动程序,就检查更新
    checkUpdate()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

# 原理

electron-updater 会根据上面setFeedURL指定路径下的latest.yml中的version来判断是否需要更新,大于当前版本的version则需要更新,否则不更新。

#electron
上次更新: 2023/09/22, 16:54:32
electron窗体操作
02.electron打包操作(electron-builder)

← electron窗体操作 02.electron打包操作(electron-builder)→

最近更新
01
uniapp常见问题
03-19
02
Vue3项目搭建
12-25
03
使用pnpm的monorepo组织项目
11-01
更多文章>
Theme by Vdoing | Copyright © 2020-2025 Mrcdh | 苏ICP备2020058908号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×