码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2021-06-03 18:18:45      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:har   received   end   body   his   set   mat   product   技术分享   

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>

<!-- All of the Node.js APIs are available in this renderer process. -->

We are using Node.js <script>document.write( process.versions.node)</script>,

and Electron <script>document.write( process.versions.electron )</script>.

<script>

// You can also require other files to run in this process

require(‘./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 = 1

setInterval( () => {

console.log(i)

i++

}, 1000)

document.getElementById(‘talk‘).www.sangpi.comaddEventListener(‘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

// 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()

})

希望本文的关于游戏分享能帮到大家!

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

标签:har   received   end   body   his   set   mat   product   技术分享   

原文地址:https://www.cnblogs.com/a252625/p/14845182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!