1,BrowserWindow中设置nodeIntegration和contextIsolation,否则无法在html中使用nodejs库
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.join(__dirname, 'preload.js')
}
})
2,在main.js中设置监听和发送
const ipcMain = electron.ipcMain;
ipcMain.on('test2', (event, arg) => {
console.log("getmsg....................");
mainWindow.webContents.send("test1", "111", "2222");
})
3,在cc发布后的index.html中赋值window.electron = require('electron');
<script type="text/javascript">
(function () {
window.electron = require('electron');
......
})();
4,在cc代码中对electron监听和发送
private electron;
private ipcRenderer;