关于electron升级调研的心得笔记

简介: 关于electron升级调研的心得笔记

调研结论

  • 应用更新官网对升级服务器有两种对应方案:具体如下方案一及方案二
  • window 环境自动更新不一定非要https http也可以。mac 下必须要是https
  • 升级过程的相关思路

    • 比较版本
    • 升级下载
    • 自动升级/手动升级

方案一:update.electronjs.org

满足条件
  • 应用运行在 macOS 或者 Windows
  • 应用有公开的 GitHub 仓库
  • 编译的版本发布在 GitHub Releases
  • 编译的版本已代码签名
参考文档

具体升级方案

自动更新功能开发时注意项

  1. 项目中用到window.print网页打印。在提升elecreon-builder版本后,打印时样式总是显示不对。elecreon-builder 的版本必须要低于20,不然就会出现这个问题。
  2. 当electron-builder 版本升级后,老的版本不能直接通过自动升级升上去。
  3. 当只跟新electron-update版本时,可以自动升级,但当当前是最新的时候点击升级会提示更新失败
  4. 当提示electron-update…至少4.0.0时,需要将electron-update放入devDependencies 中。

方案二:构建自己的更新服务器

参考文档

具体方案


客户端

升级方案

调研结论

  • 应用更新官网对升级服务器有两种对应方案:具体如下方案一及方案二
  • window 环境自动更新不一定非要https http也可以。mac 下必须要是https
  • 升级过程的相关思路

    • 比较版本
    • 升级下载
    • 自动升级/手动升级

方案一:update.electronjs.org

满足条件
  • 应用运行在 macOS 或者 Windows
  • 应用有公开的 GitHub 仓库
  • 编译的版本发布在 GitHub Releases
  • 编译的版本已代码签名
参考文档

具体升级方案

自动更新功能开发时注意项

  1. 项目中用到window.print网页打印。在提升elecreon-builder版本后,打印时样式总是显示不对。elecreon-builder 的版本必须要低于20,不然就会出现这个问题。
  2. 当electron-builder 版本升级后,老的版本不能直接通过自动升级升上去。
  3. 当只跟新electron-update版本时,可以自动升级,但当当前是最新的时候点击升级会提示更新失败
  4. 当提示electron-update…至少4.0.0时,需要将electron-update放入devDependencies 中。

方案二:构建自己的更新服务器

参考文档

具体方案


客户端

升级方案

升级方案流程
升级方案流程

此方案实现参考地址

其他写法具体方案

安装==electron-updater==

npm install electron-updater --save

修改packpage.json

"publish": [
      {
        "provider": "generic",
        "url": "http://自己ip/app/update"
      }
    ],
核心方法

主进程 主要是==handleUpdate==方法

import {app, BrowserWindow, ipcMain} from 'electron'
// 注意这个autoUpdater不是electron中的autoUpdater
import {autoUpdater} from "electron-updater"

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
    global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
    ? `http://localhost:9080`
    : `file://${__dirname}/index.html`

function createWindow() {
    /**
     * Initial window options
     */
    mainWindow = new BrowserWindow({
        height: 563,
        useContentSize: true,
        width: 1000
    })

    mainWindow.loadURL(winURL)

    mainWindow.on('closed', () => {
        mainWindow = null
    });


//处理更新操作
    function handleUpdate() {
        const returnData = {
            error: {status: -1, msg: '检测更新查询异常'},
            checking: {status: 0, msg: '正在检查应用程序更新'},
            updateAva: {status: 1, msg: '检测到新版本,正在下载,请稍后'},
            updateNotAva: {status: -1, msg: '您现在使用的版本为最新版本,无需更新!'},
        };

        //和之前package.json配置的一样
        autoUpdater.setFeedURL('http://xxx.com/app/update');

        //更新错误
        autoUpdater.on('error', function (error) {
            sendUpdateMessage(returnData.error)
        });

        //检查中
        autoUpdater.on('checking-for-update', function () {
            sendUpdateMessage(returnData.checking)
        });

        //发现新版本
        autoUpdater.on('update-available', function (info) {
            sendUpdateMessage(returnData.updateAva)
        });

        //当前版本为最新版本
        autoUpdater.on('update-not-available', function (info) {
            setTimeout(function () {
                sendUpdateMessage(returnData.updateNotAva)
            }, 1000);
        });

        // 更新下载进度事件
        autoUpdater.on('download-progress', function (progressObj) {
            mainWindow.webContents.send('downloadProgress', progressObj)
        });


        autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
            ipcMain.on('isUpdateNow', (e, arg) => {
                //some code here to handle event
                autoUpdater.quitAndInstall();
            });
            // win.webContents.send('isUpdateNow')
        });

        //执行自动更新检查
        autoUpdater.checkForUpdates();
    }

    handleUpdate();

// 通过main进程发送事件给renderer进程,提示更新信息
    function sendUpdateMessage(text) {
        mainWindow.webContents.send('message', text)
    }

    ipcMain.on("checkForUpdate", (event, data) => {
        console.log('执行自动更新检查!!!');
        // event.sender.send('reply', 'hi lee my name is yuan, age is 17');
        autoUpdater.checkForUpdates();
    });
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
});

app.on('activate', () => {
    if (mainWindow === null) {
        createWindow()
    }
});

有更新包即会触发

  autoUpdater.on('download-progress', function (progressObj) {
        // mainWindow.webContents.send('downloadProgress', progressObj)
        const winId = BrowserWindow.getFocusedWindow().id;
        let win = BrowserWindow.fromId(winId);
        win.webContents.send('downloadProgress', progressObj)
    });
    

progressObj 返回的对象

  • bytesPerSecond: bps/s //传送速率
  • percent : 百分比
  • total : 总大小
  • transferred: 已经下载

使用==ipcRenderer==用于监听和发送消息向主进程
api官网地址

ipcRenderer.on 监听事件
ipcRenderer.send 发送事件
此方案实现参考地址

其他写法具体方案

安装==electron-updater==

npm install electron-updater --save

修改packpage.json

"publish": [
      {
        "provider": "generic",
        "url": "http://自己ip/app/update"
      }
    ],
核心方法

主进程 主要是==handleUpdate==方法

import {app, BrowserWindow, ipcMain} from 'electron'
// 注意这个autoUpdater不是electron中的autoUpdater
import {autoUpdater} from "electron-updater"

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
    global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
    ? `http://localhost:9080`
    : `file://${__dirname}/index.html`

function createWindow() {
    /**
     * Initial window options
     */
    mainWindow = new BrowserWindow({
        height: 563,
        useContentSize: true,
        width: 1000
    })

    mainWindow.loadURL(winURL)

    mainWindow.on('closed', () => {
        mainWindow = null
    });


//处理更新操作
    function handleUpdate() {
        const returnData = {
            error: {status: -1, msg: '检测更新查询异常'},
            checking: {status: 0, msg: '正在检查应用程序更新'},
            updateAva: {status: 1, msg: '检测到新版本,正在下载,请稍后'},
            updateNotAva: {status: -1, msg: '您现在使用的版本为最新版本,无需更新!'},
        };

        //和之前package.json配置的一样
        autoUpdater.setFeedURL('http://xxx.com/app/update');

        //更新错误
        autoUpdater.on('error', function (error) {
            sendUpdateMessage(returnData.error)
        });

        //检查中
        autoUpdater.on('checking-for-update', function () {
            sendUpdateMessage(returnData.checking)
        });

        //发现新版本
        autoUpdater.on('update-available', function (info) {
            sendUpdateMessage(returnData.updateAva)
        });

        //当前版本为最新版本
        autoUpdater.on('update-not-available', function (info) {
            setTimeout(function () {
                sendUpdateMessage(returnData.updateNotAva)
            }, 1000);
        });

        // 更新下载进度事件
        autoUpdater.on('download-progress', function (progressObj) {
            mainWindow.webContents.send('downloadProgress', progressObj)
        });


        autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
            ipcMain.on('isUpdateNow', (e, arg) => {
                //some code here to handle event
                autoUpdater.quitAndInstall();
            });
            // win.webContents.send('isUpdateNow')
        });

        //执行自动更新检查
        autoUpdater.checkForUpdates();
    }

    handleUpdate();

// 通过main进程发送事件给renderer进程,提示更新信息
    function sendUpdateMessage(text) {
        mainWindow.webContents.send('message', text)
    }

    ipcMain.on("checkForUpdate", (event, data) => {
        console.log('执行自动更新检查!!!');
        // event.sender.send('reply', 'hi lee my name is yuan, age is 17');
        autoUpdater.checkForUpdates();
    });
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
});

app.on('activate', () => {
    if (mainWindow === null) {
        createWindow()
    }
});

有更新包即会触发

  autoUpdater.on('download-progress', function (progressObj) {
        // mainWindow.webContents.send('downloadProgress', progressObj)
        const winId = BrowserWindow.getFocusedWindow().id;
        let win = BrowserWindow.fromId(winId);
        win.webContents.send('downloadProgress', progressObj)
    });
    

progressObj 返回的对象

  • bytesPerSecond: bps/s //传送速率
  • percent : 百分比
  • total : 总大小
  • transferred: 已经下载

使用==ipcRenderer==用于监听和发送消息向主进程
api官网地址

ipcRenderer.on 监听事件
ipcRenderer.send 发送事件

相关文章
|
2月前
|
JavaScript Shell API
小笔记:Electron中关联格式、处理文件、创建链接的相关编程
小笔记:Electron中关联格式、处理文件、创建链接的相关编程
125 0
|
2月前
|
存储 Web App开发 iOS开发
Electron 从基础到实战笔记 - Electron App对象及其事件
Electron 从基础到实战笔记 - Electron App对象及其事件
138 0
|
JavaScript Shell API
笔记:Electron中关联格式、处理文件、创建链接的相关编程
本文介绍关于在Electron中绑定文件格式、在菜单中打开、使用文件API、处理桌面链接等等。
140 0
|
前端开发 JavaScript
electron入门笔记(三)- 引入bootstrap
源码:https://github.com/sueRimn/electron-bootstrap 当引入jQuery和bootstrap文件时,会报错,原因是:electron 的 Renderer 端因为注入了 Node 环境,存在全局函数 require,导致jquery 内部环境判断出现问题。
2142 0
|
Web App开发 JavaScript 索引
Electron入门笔记(一)-自己快速搭建一个app demo
一.安装Node   1.从node官网下载 ,最好安装.msi后缀名的文件,新手可以查看安装教程进行安装。 2.然后cmd进入命令窗口,输入npm -v查看node当前版本, 二.创建文件并初始化   1.
2430 0
|
Web App开发
Electron入门笔记(二)-快速建立hello world
官方的文档我没有看懂,看了不少别人的博客和文章,终于慢慢看懂了如何快速的建立一个Electron app demo,前一篇文章不是使用官方快速搭建的,而且还出了小问题,所以去撸了一遍quick-start,发现很简单 一、安装Electron 1.
1051 0
|
Web App开发 JavaScript
|
2月前
|
移动开发 开发框架 JavaScript
Vue3 Vite electron 开发桌面程序
Vue3 Vite electron 开发桌面程序
155 0
|
8月前
|
前端开发 算法 JavaScript
从零开始开发图床工具:使用 Gitee 和 Electron 实现上传、管理和分享(下)
从零开始开发图床工具:使用 Gitee 和 Electron 实现上传、管理和分享(下)
82 0
|
8月前
|
存储 Web App开发 JavaScript
从零开始开发图床工具:使用 Gitee 和 Electron 实现上传、管理和分享(上)
从零开始开发图床工具:使用 Gitee 和 Electron 实现上传、管理和分享(上)
125 0