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;
start() {
console.log(window['test']);
this.electron = window['electron'];
this.ipcRenderer = this

通过设置BrowserWindow的nodeIntegration和contextIsolation,结合main.js中的监听与发送,以及在CocosCreator发布的index.html引入electron库,经过一番努力成功实现了Electron与CocosCreator之间的通信。
最低0.47元/天 解锁文章
1002





