- package.json
"build": { "appId": "com.whbs.app", "productName": "whbs", "directories": { "output": "build" }, "dmg": { "contents": [ { "x": 410, "y": 150, "type": "link", "path": "/Applications" }, { "x": 130, "y": 150, "type": "file" } ] }, "mac": { "icon": "./assets/icons/mac.icns", "target": [ "zip", "dmg" ], "publish": [ { "provider": "generic", "url": "" //服務器地址,熱更新使用 } ] }, "win": { "icon": "./assets/icons/win.ico", "target": [ "nsis", "zip" ], "publish": [ { "provider": "generic", "url": "" //服務器地址,熱更新使用 } ] }, "linux": { "icon": "./assets/icons" }, "nsis": { "oneClick": false, "allowToChangeInstallationDirectory": true, "perMachine": true, "artifactName": "${productName}-setup-${version}.${ext}" } }
- 主進程
const { app, BrowserWindow, ipcMain} = require('electron'); const { autoUpdater } = require('electron-updater'); // path是node 內置模塊 拼接路徑 const path = require('path'); let win, webContents; // 創建窗口 // 自定義方法 function ml_createrWindwo () { // 創建窗口 win = new BrowserWindow({ webPreferences:{ nodeIntegration: true, }, titleBarStyle: 'hidden', frame: false, resizable: false, show: false, width:880, height:540, }); // win.maximize(); // 加載內容 // 加載遠程地址 // win.loadURL('http://www.baidu.com/') // 加載本地 // __dirname :當前JS文件所在文件路徑,絕對路徑 // 相對路徑 win.loadURL(path.join(__dirname,'./index.html')) // max 系統的 :win.loadURL(path.join('file://',__dirname,'./index.html')) webContents = win.webContents; win.once('ready-to-show', () => { win.show() }) // 關閉窗口 win.on('close',function () { //TODO: 關閉窗口前想做的事 win = null; }) //監聽窗口最大化 win.on('maximize', () => { win.webContents.send('maximize'); }) //監聽窗口最小化 win.on('unmaximize', () => { win.webContents.send('unmaximize'); }) } // 主進程監聽渲染進程傳來的信息 ipcMain.on('update', (e, arg) => { ml_updateHandle(); }); //檢查更新 function ml_updateHandle () { //設置檢查更新的 url,並且初始化自動更新。這個 url 一旦設置就無法更改。 const updateFeedUrl='http://*************'; //package.json配置的服務器地址 autoUpdater.setFeedURL(updateFeedUrl); autoUpdater.on('error', function(message){ sendUpdateMessage('error', message); }); //當開始檢查更新的時候觸發 autoUpdater.on('checking-for-update', function(message) { sendUpdateMessage('checking-for-update', message); }); //當發現一個可用更新的時候觸發,更新包下載會自動開始 autoUpdater.on('update-available', function(message) { console.log("update-available"); sendUpdateMessage('update-available', message); }); //當沒有可用更新的時候觸發 autoUpdater.on('update-not-available', function(message) { console.log("update-not-available") sendUpdateMessage('update-not-available', message); }); // 更新下載進度事件 autoUpdater.on('download-progress', function(progressObj) { console.log("download-progress") sendUpdateMessage('downloadProgress', progressObj); }) /** * event Event * releaseNotes String - 新版本更新公告 * releaseName String - 新的版本號 * releaseDate Date - 新版本發佈的日期 * updateURL String - 更新地址 * */ autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) { sendUpdateMessage('isUpdateNow'); ipcMain.on('updateNow', (e, arg) => { //在下載完成後,重啓當前的應用並且安裝更新 autoUpdater.quitAndInstall(); }); //在下載完成後,重啓當前的應用並且安裝更新 // autoUpdater.quitAndInstall(); //通過main進程發送事件給renderer進程,提示更新信息 //mainWindow.webContents.send('isUpdateNow') }); //執行自動更新檢查 autoUpdater.checkForUpdates(); } // 主進程主動發送消息給渲染進程函數 function sendUpdateMessage(message, data) { console.log({ message, data }); webContents.send('message', { message, data }); }
electron + nodejs (熱更新)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.