http://git.1473.cn/root/UseStudio.Browers
const electron = require('electron'); const path = require('path'); const url = require('url'); const autoUpdater = require('electron-updater').autoUpdater; const ipc = electron.ipcMain; const {ipcMain} = require('electron'); const app = electron.app; const BrowserWindow = electron.BrowserWindow;
// 检测更新,在你想要检查更新的时候执行,renderer事件触发后的操作自行编写 function updateHandle(){ //minimize ipcMain.on('hide-window', () => { mainWindow.minimize(); }); //maximize ipcMain.on('show-window', () => { mainWindow.maximize(); }); //unmaximize ipcMain.on('orignal-window', () => { mainWindow.unmaximize(); }); //打开默认浏览器 ipcMain.on('open-office-website', function(event, arg){ shell.openExternal(arg) }) ipcMain.on('check-for-update', function(event, arg) { let message={ appName:'有思浏览器', error:'检查更新出错, 请联系开发人员', checking:'正在检查更新……', updateAva:'检测到新版本,正在下载……', updateNotAva:'现在使用的就是最新版本,不用更新', downloaded: '最新版本已下载,点击安装已进行更新' }; //设置检查更新的 url,并且初始化自动更新。这个 url 一旦设置就无法更改。 const updateFeedUrl='http://client.1473.cn/update'; if(os.platform()==='darwin'){ updateFeedUrl='http://client.1473.cn/update'; } autoUpdater.setFeedURL(updateFeedUrl); autoUpdater.on('error', function(error){ return dialog.showMessageBox(mainWindow, { type: 'info', buttons: ['OK'], title: message.appName, message: message.errorTips, detail: '\r' + message.error }); sendUpdateMessage(message.error) }); //当开始检查更新的时候触发 autoUpdater.on('checking-for-update', function() { sendUpdateMessage(message.checking) return dialog.showMessageBox(mainWindow, { type: 'info', buttons: ['OK'], title: message.appName, message: message.checking }); }); //当发现一个可用更新的时候触发,更新包下载会自动开始 autoUpdater.on('update-available', function(info) { sendUpdateMessage(message.updateAva) var downloadConfirmation = dialog.showMessageBox(mainWindow, { type: 'info', buttons: ['OK'], title: message.appName, message: message.updateAva }); if (downloadConfirmation === 0) { return; } }); //当没有可用更新的时候触发 autoUpdater.on('update-not-available', function(info) { return dialog.showMessageBox(mainWindow, { type: 'info', buttons: ['OK'], title: message.appName, message: message.updateNotAva }); sendUpdateMessage(message.updateNotAva) }); // 更新下载进度事件 autoUpdater.on('download-progress', function(progressObj) { mainWindow.webContents.send('downloadProgress', progressObj) }) /** * event Event * releaseNotes String - 新版本更新公告 * releaseName String - 新的版本号 * releaseDate Date - 新版本发布的日期 * updateURL String - 更新地址 * */ autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) { var index = dialog.showMessageBox(mainWindow, { type: 'info', buttons: ['现在重启', '稍后重启'], title: message.appName, message: message.downloaded, //detail: releaseName + "\n\n" + releaseNotes }); console.log(index); if (index === 1) return; //在下载完成后,重启当前的应用并且安装更新 autoUpdater.quitAndInstall(); //通过main进程发送事件给renderer进程,提示更新信息 //mainWindow.webContents.send('isUpdateNow') }); //执行自动更新检查 autoUpdater.checkForUpdates(); }); }
4.1 主线程与渲染线程之间通信
ipcr.send('check-for-update', 'event-update');
4.2 触发主线程,检查更新 autoUpdater各种状态
执行check-for-update
4.3 使用electron-builder打包electron应用
//electron-builder和electron-packager基本类似,不过builder打包完成的是安装包,而packager打包完成的是可执行文件,packager里面有项目源码,builder里面则是编译后的。这可能是两者的最大差别。 { "name": "electron-build", "version": "1.0.0", "main": "src/main.js", "description": "electron-build project", "author": "ZJINH", "license": "", "dependencies": { "electron-updater": "^2.21.10", "fs": "0.0.1-security", "regedit": "^3.0.0" }, "dependencies": { "electron-updater": "^1.4.2" }, "scripts": { "pack": "electron-builder --dir", "build": "electron-builder", "dev": "electron src/main.js" }, "keywords": [ "electron", "updater", "update", "mac", "osx", "linux", "desktop" ], "build": { "appId": "com.cilent.app.electronbuild", "productName": "electron-build", "directories": { "output": "build" }, "files": [ "src/**/*", "node_modules/**/*", "package.json" ], "dmg": { "contents": [ { "x": 410, "y": 150, "type": "link", "path": "/Applications" }, { "x": 130, "y": 150, "type": "file" } ] }, "mac": { "category": "your.app.category.type", "icon": "static/icons/app.icns", "target": [ "zip", "dmg" ], "publish": [ { "provider":"generic", "url":"http://client.1473.cn/update/latest/mac/" } ] }, "win": { "icon": "static/icons/icon.ico", "target": [ "nsis", "zip" ], "publish": [ { "provider":"generic", "url":"http://client.1473.cn/update/latest/win/" } ] }, "linux": { "icon": "static/icons" }, "nsis":{ "oneClick":true, "artifactName":"${productName}-setup-${version}.${ext}" } } }
作者:信息录入:文案编辑:视频录制:视频编辑:图片编辑: