日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

web前端技术分享Electron之IPC 通信

發(fā)布時間:2024/9/30 HTML 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 web前端技术分享Electron之IPC 通信 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文由小千給大家分享Electron之IPC 通信。

1、index.html

<!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 processrequire('./renderer.js')</script></body></html>

2、renderer.js

// 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 = 1setInterval( () => {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

// Modulesconst {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 readyfunction createWindow () {mainWindow = new BrowserWindow({width: 1000, height: 800, x: 100, y:140,webPreferences: { nodeIntegration: true }})// Load index.html into the new BrowserWindowmainWindow.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 closedmainWindow.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 readyapp.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 BrowserWindowapp.on('activate', () => {if (mainWindow === null) createWindow()})

希望本文的分享能幫到大家!本文來自千鋒教育,轉(zhuǎn)載請注明出處。

總結(jié)

以上是生活随笔為你收集整理的web前端技术分享Electron之IPC 通信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。