Electron使用指南——IPC 通信
2020-08-28 13:31
597 查看
IPC 通信
1、index.html
[code]<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'"> <title>Hello World!</title> </head> <body> <h1>Hello World!</h1> <button type="button" id="talk">Talk to main process</button><br> <!-- All of the Node.js APIs are available in this renderer process. --> We are using Node.js <strong><script>document.write( process.versions.node)</script></strong>, and Electron <strong><script>document.write( process.versions.electron )</script></strong>. <script> // You can also require other files to run in this process require('./renderer.js') </script> </body> </html>
2、renderer.js
[code]// This file is required by the index.html file and will // be executed in the renderer process for that window. // All of the Node.js APIs are available in this process. const { ipcRenderer } = require('electron') let i = 1 setInterval( () => { console.log(i) i++ }, 1000) document.getElementById('talk').addEventListener('click', e => { // ipcRenderer.send( 'channel1', 'Hello from main window') let response = ipcRenderer.sendSync( 'sync-message', 'Waiting for response') console.log(response) }) ipcRenderer.on( 'channel1-response', (e, args) => { console.log(args) }) ipcRenderer.on( 'mailbox', (e, args) => { console.log(args) })
3、main.js
[code]// Modules const {app, BrowserWindow, ipcMain} = require('electron') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let mainWindow // Create a new BrowserWindow when `app` is ready function createWindow () { mainWindow = new BrowserWindow({ width: 1000, height: 800, x: 100, y:140, webPreferences: { nodeIntegration: true } }) // Load index.html into the new BrowserWindow mainWindow.loadFile('index.html') // Open DevTools - Remove for PRODUCTION! mainWindow.webContents.openDevTools(); mainWindow.webContents.on( 'did-finish-load', e => { // mainWindow.webContents.send( 'mailbox', { // from: 'Ray', // email: 'ray@stackacademy.tv', // priority: 1 // }) }) // Listen for window being closed mainWindow.on('closed', () => { mainWindow = null }) } ipcMain.on( 'sync-message', (e, args) => { console.log(args) setTimeout( () => { e.returnValue = 'A sync response from the main process' }, 4000) }) ipcMain.on( 'channel1', (e, args) => { console.log(args) e.sender.send( 'channel1-response', 'Message received on "channel1". Thank you!') }) // Electron `app` is ready app.on('ready', createWindow) // Quit when all windows are closed - (Not macOS - Darwin) app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() }) // When app icon is clicked and app is running, (macOS) recreate the BrowserWindow app.on('activate', () => { if (mainWindow === null) createWindow() })
相关文章推荐
- 张高兴的 .NET Core IoT 入门指南:(四)使用 SPI 进行通信
- IPC用户指南/MessageQ模块的使用
- Android使用Service实现IPC通信的2种方式
- 张高兴的 .NET Core IoT 入门指南:(三)使用 I2C 进行通信
- [Linux管道和IPC]使用信号量和共享内存进行父子进程通信
- Electron使用指南 - [01] 介绍
- Volley使用指南(android通信框架)
- [Android]IPC通信之Messenger的使用方法介绍
- Electron使用指南 - [02] 搭建 Electron 运行环境
- 张高兴的 .NET Core IoT 入门指南:(三)使用 I2C 进行通信
- Beetle框架使用指南之--远程通信模块
- 使用AIDL实现IPC通信之——实现服务端主动发送数据给客户端
- AIDL/IPC Android AIDL/IPC 进程通信机制——超具体解说及使用方法案例剖析(播放器)
- IPC机制--使用文件通信
- 张高兴的 .NET Core IoT 入门指南:(四)使用 SPI 进行通信
- 【资料】IBM MQSeries使用指南--配置一个能够通信的远程连接
- electron窗口和主进程通信ipc
- 关于使用ftok函数生成IPC通信所需key值的个人理解
- 【Android开发艺术探索】IPC机制(四)-使用AIDL进行跨进程通信
- 跨进程通信使用总结(二)_Android中的IPC方式