electron + nodejs (热更新)

  1. 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}"
    		}
    	}

     

  2. 主进程
     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 });
    }

     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章