最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error when interconnecting ipcRenderer and ipcMain in electron - Stack Overflow

programmeradmin1浏览0评论

I have configured cli angular in electron, and I have a link where it execute a function that interunicate ipcRenderer and ipcMain:

html:

<a (click)="check()"> click </a>

ponent:

constructor(private _e: ElectronService) { }

check () {
   this._e.ipcRenderer.send ('conn', 'round');
   this._e.ipcRenderer.on ('conn-st', (event, args) => {
      console.log (args);
   });
}

main.js (electron):

ipcMain.on ('conn', function (event, args) {
  event.sender.send ('conn-st', 'trip');
});

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.

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

How do I end the connection between ipcRenderer and ipcMain?

I have configured cli angular in electron, and I have a link where it execute a function that interunicate ipcRenderer and ipcMain:

html:

<a (click)="check()"> click </a>

ponent:

constructor(private _e: ElectronService) { }

check () {
   this._e.ipcRenderer.send ('conn', 'round');
   this._e.ipcRenderer.on ('conn-st', (event, args) => {
      console.log (args);
   });
}

main.js (electron):

ipcMain.on ('conn', function (event, args) {
  event.sender.send ('conn-st', 'trip');
});

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.

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

How do I end the connection between ipcRenderer and ipcMain?

Share Improve this question asked Mar 28, 2017 at 15:33 Vladimir J. Castañeda G.Vladimir J. Castañeda G. 933 silver badges8 bronze badges 6
  • There actually is no "connection", like UNIX sockets, Electron will emit a signal to all listeners which then trigger the defined function. The error message appears to only say that there were 11 listeners created, which all listen to the same "socket". – Alexander Leithner Commented Mar 28, 2017 at 15:49
  • And how do I restart that listeners? – Vladimir J. Castañeda G. Commented Mar 28, 2017 at 16:01
  • "Restart" is maybe the wrong word, because it means they would keep listening after that. But every listener (as created) with ipcRenderer.on(); creates a unique id and can be removed with ipcRenderer.removeListener(channel, listener);. But please refer to the Electron doc. – Alexander Leithner Commented Mar 28, 2017 at 16:05
  • Thanks a lot for helping more about electron, i solved it with this: this._e.ipcRenderer.send ('conn', 'test'); this._e.ipcRenderer.on ('conn-st', (event, args) => { console.log (args); this._e.ipcRenderer.removeAllListeners ('conn-st'); }); – Vladimir J. Castañeda G. Commented Mar 28, 2017 at 16:30
  • You are very wele. Should I create an answer so that you can accept it? – Alexander Leithner Commented Mar 28, 2017 at 17:01
 |  Show 1 more ment

2 Answers 2

Reset to default 6

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");

This is also covered in the Electron documentation, particularly here.

The accepted answer is nolonger correct as per the electron documentation documentation. The listener is a function and should be removed as shown below.

// Create a listener
let listener = (event, args) => {}
ipcRenderer.on("channel", listener );

//Delete the listener
ipcRenderer.removeListener("parse-cm-request", listener);
发布评论

评论列表(0)

  1. 暂无评论