问题描述
我已经在电子中配置了cli角,并且我有一个链接可以执行使ipcRenderer和ipcMain互通的功能:
I have configured cli angular in electron, and I have a link where it execute a function that intercomunicate ipcRenderer and ipcMain:
html:
<a (click)="check()"> click </a>
组件:
constructor(private _e: ElectronService) { }
check () {
this._e.ipcRenderer.send ('conn', 'round');
this._e.ipcRenderer.on ('conn-st', (event, args) => {
console.log (args);
});
}
main.js(电子):
main.js (electron):
ipcMain.on ('conn', function (event, args) {
event.sender.send ('conn-st', 'trip');
});
问题在于,当您单击一次时,它会执行一次,但是当您再次单击时,它会连续执行3、4、5,依此类推.
The problem is that when you click once, you do it once, but when you click again it does 3, then 4, 5 and so on continuously.
并在达到11时抛出此错误:
And throws this error upon reaching 11:
(node:23006) Error: Possible EventEmitter memory leak detected. 11 conn-st listeners added. Use emitter.setMaxListeners() to increase limit
如何结束ipcRenderer和ipcMain之间的连接?
How do I end the connection between ipcRenderer and ipcMain?
推荐答案
该错误消息仅表明,已创建11个套接字"(如UNIX中的监听器)的侦听器.每个侦听器都会创建一个唯一的ID,该ID在创建侦听器时会返回.基于此,可以这样删除一个特定的侦听器:
That error message only says, that 11 listeners to a "socket" (like the ones in UNIX) were created. Every listener creates a unique ID which is returned when creating the listener. Based on that, removing one particular listener could be done like this:
// Create a listener.
var myListener = function (event, args) {}
ipcRenderer.on("channel", myListener);
// Delete only this one by its ID:
ipcRenderer.removeListener("channel", myListener);
但是您也可以删除为套接字创建的所有侦听器,如下所示:
But you can also delete all of the listeners that were created for a socket, like this:
// Create a few listeners.
var myListener0 = function (event, args) {};
var myListener1 = function (event, args) {};
var myListener2 = function (event, args) {};
var myListener3 = function (event, args) {};
//
ipcRenderer.on("channel", myListener0);
ipcRenderer.on("channel", myListener1);
ipcRenderer.on("channel", myListener2);
ipcRenderer.on("channel", myListener3);
// Delete all listeners for socket "channel".
ipcRenderer.removeAllListeners("channel");
Electron文档中也对此进行了介绍,尤其是此处.
This is also covered in the Electron documentation, particularly here.
这篇关于在电子中互连ipcRenderer和ipcMain时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!