Electron 的线程通信
主线程:Main
渲染线程:Renderer
Main --> Renderer
https://electronjs.org/docs/a...
Main
const {app, BrowserWindow} = require('electron') let win = null app.on('ready', () => { win = new BrowserWindow({width: 800, height: 600}) win.loadURL(`file://${__dirname}/index.html`) win.webContents.on('did-finish-load', () => { win.webContents.send('ping', 'whoooooooh!') }) })
Renderer
<html> <body> <script> require('electron').ipcRenderer.on('ping', (event, message) => { console.log(message) // Prints 'whoooooooh!' }) </script> </body> </html>
Renderer --> Main
https://electronjs.org/docs/a...
Renderer
// synchronous const {ipcRenderer} = require('electron') console.log(ipcRenderer.sendSync('synchronous-message', 'ping')) // prints "pong" // asynchronous ipcRenderer.on('asynchronous-reply', (event, arg) => { console.log(arg) // prints "pong" }) ipcRenderer.send('asynchronous-message', 'ping')
Main
// synchronous ipcMain.on('synchronous-message', (event, arg) => { console.log(arg) // prints "ping" event.returnValue = 'pong' }) // asynchronous const {ipcMain} = require('electron') ipcMain.on('asynchronous-message', (event, arg) => { console.log(arg) // prints "ping" event.sender.send('asynchronous-reply', 'pong') })
Global Object
Main
// Define global.sharedObject = { someProperty: 'default value' } // Get var a = global.sharedObject.someProperty // Set global.sharedObject = 'custom value'
Renderer
const electron = require('electron') // Get var a = electron.remote.getGlobal('sharedObject').someProperty // Set electron.remote.getGlobal('sharedObject').someProperty = 'new value'
相关推荐
游走的豚鼠君 2020-11-10
sanallen 2020-07-04
electronvolt 2020-07-04
sanallen 2020-06-14
moyigg 2020-06-09
疯狂老司机 2020-06-07
zhujuyu 2020-06-06
moyigg 2020-06-01
zhujuyu 2020-05-30
viewerlin 2020-05-29
zhujuyu 2020-05-28
yanchuncheng 2020-05-12
Trustport 2020-05-06
chenyijun 2020-05-05
electronvolt 2020-05-04
游走的豚鼠君 2020-05-01
electronvolt 2020-04-21
游走的豚鼠君 2020-04-18