In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what the principle of Electron is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
In Electron applications, GUI components are available only in the main process and not in the secondary process. Then if you want to try the GUI component in the worker process, you must communicate with the main process. The ipc module is used to realize the communication between the main process and the auxiliary process. In the main process, the ipcMain module is used to control and deal with the communication of the auxiliary process. In the worker process, the ipcRenderer module is used to send a message to or receive a response from the main process. Next, let's talk about how the two modules communicate.
In the main process, the ipcMain module is used to control and deal with the communication of the auxiliary process. The ipcMain module is an instance of EventEmitter. The ipcMain module has four listening methods:
IpcMain.on (channel, listener)
Parsing: listens for the event channel, and when a new message arrives, the listening method listener (event, args) is executed.
IpcMain.once (channel, listener)
Parsing: add a listening method listener that executes only once to the event channel. Listener is called when the first message arrives, and the listener is deleted immediately after execution.
IpcMain.removeListenner (channel, listener)
Parsing: removes a specific channel listener from a specific listener listener event.
IpcMain.removeAllListeners ([channel])
Analysis: this method can accept the parameter channel. If the parameter channel is passed, all listeners of a specific channel will be deleted. If no parameters are passed, all listeners are deleted.
There are two main ways to pass the event object to callback,event: synchronous reply message and asynchronous reply message
Event.returnValue
Parsing: this method can be used when the main process needs to reply a synchronization message to the worker process.
Event.sender.send ('message')
Parsing: this method can be used when the main process needs to reply an asynchronous message to the worker process.
Let's continue to use yesterday's project, let's take a look at how the main process communicates with the secondary process:
First of all, the main process we talked about is the index.js file, in which we use the ipcMain module to process messages:
Const ipcMain = require ('electron'). IpcMain
IpcMain.on ('test-message', function (event, arg) {event.sender.send (' test-reply', 'this is the main process');})
IpcMain.on ('test-message', function (event, arg) {event.returnValue =' this is the main process';})
The worker process currently has only one index.html page, in which we use ipcRenderer to process messages
Const ipcRenderer = require ('electron'). IpcRenderer
Console.log (ipcRenderer.sendSync ('test-message',' this is the worker process'); ipcRenderer.on ('test-reply', function (event, arg) {console.log (arg);}); ipcRenderer.send (' test-message', 'this is the worker process')
We use electron. Command to test the effect:
You can see that the main process has successfully communicated, so let's talk about the specific use of ipcRenderer in the worker process.
The worker process is actually equivalent to our individual html files, and you can use ipcRenderer to receive and send messages to interact with the main process.
There are four methods that the ipcRenderer module uses to listen for events:
IpcRenderer.on (channel, listener) ipcRenderer.once (channel, listener) ipcRenderer.removeListenner (channel, listener) ipcRenderer.removeAllListeners ([channel])
The meaning of these four methods is the same as that of ipcMain, so I won't repeat it here. Sending message ipcRenderer has two methods: sending synchronous message and sending asynchronous message:
IpcRenderer.send (channel, args)
Parsing: the worker process uses this method to send asynchronous messages to the main process. The main process uses the ipcMain module to process asynchronous messages.
IpcRenderer.sendSync (channel, args)
Parsing: the worker process uses this method to send synchronization messages to the main process. The main process uses the ipcMain module to process the synchronization message and then sends the corresponding message to the helper process through event.returnValue. My advice here is to use as few synchronization messages as possible, because sending synchronization messages can block the entire page rendering process, which is a very bad thing for the user experience.
IpcRenderer actually has another way to send messages:
IpcRenderer.sendToHost (channel, args) on what the principle of Electron is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.