电子IPC消息过多
Electron IPC excessive messages
我正在尝试在Electron (atom shell)平台上构建一个桌面应用程序,目前在mac os x上。
我正在尝试它的IPC(进程间通信)模块,用于在两个主要电子进程,主进程和渲染进程之间发送和接收同步和异步消息。
然而,对于异步消息,我得到的消息比预期的要多,因为我已经从渲染进程发送到主进程,在主进程中它们被回复。我知道这是从控制台输出的,主进程的应答应该产生的。
对于DOM的2个组件中的每一个,我向主进程发送1条消息,它用一个记录到控制台的回复来响应。但是对于2个组件(react组件),我得到4个控制台日志行,对于3个组件,它是9,对于4个组件,它是16个控制台日志消息行。
这是怎么回事?我错过了什么关于异步消息和IPC回复?同步消息和回复没有问题。
main.js(主进程代码):var app = require('app'); // Module to control application life.
var BrowserWindow = require('browser-window'); // Module to create native browser window.
var ipc = require('ipc');
// Report crashes to our server.
require('crash-reporter').start();
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is GCed.
var mainAppWindow = null;
var bookWindow = null;
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
app.quit();
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
// Create the browser window.
mainAppWindow = new BrowserWindow({width: 1200, height: 900, 'title-bar-style': 'hidden'});
// and load the index.html of the app.
mainAppWindow.loadUrl('file://' + __dirname + '/index.html');
mainAppWindow.openDevTools();
// Emitted when the window is closed.
mainAppWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainAppWindow = null;
app.quit();
});
ipc.on('asynchronous-message', function(event, arg) {
console.log(arg); // prints "ping"
event.sender.send('asynchronous-reply', 'pong');
});
// listen for the messages from renderer process to close the mainwindow and open a readerwindow
ipc.on('synchronous-message', function(event, arg) {
console.log(arg); // prints "ping"
event.returnValue = 'pong';
});
// when all of the book reader windows are closed open the mainAppWindow
// in main process listen for all bookreaderwindows are closed
// and in each, checking an array of them formed when they are each created.
});
index.js(渲染程序代码):
/*
-mainApp
-Controls
-Books
-Book
*/
var ipc = require('ipc');
var Book = React.createClass({
switchToBookReader: function() {
// close mainAppWindow, and open bookreader window.
// send message to main process 'close window'.
// listen in main process for this.
// callback for this in main process is to close the main window and open a readerwindow
},
componentDidMount: function() {
ipc.send('asynchronous-message', 'ping');
ipc.on('asynchronous-reply', function(arg) {
console.log(arg); // prints "pong"
});
console.log(ipc.sendSync('synchronous-message', 'ping')); // prints "pong"
},
render: function() {
return (
<div onDoubleClick={this.switchToBookReader()} >
{this.props.name}
</div>
);
}
});
var Books = React.createClass({
render: function() {
// create Book nodes here.
var bookNodes = [];
bookNodes.push(<Book name="book1"/>);
bookNodes.push(<Book name="book2"/>);
return (
<div>
{bookNodes}
</div>
);
}
});
var Controls = React.createClass({
render: function() {
return (
<div>
Controls
</div>
);
}
});
var mainApp = React.createClass({
render: function() {
return (
<div>
<Controls />
<Books />
</div>
);
}
});
React.render(<mainApp />, document.body);
每个侦听器创建一个唯一的ID,该ID在创建侦听器时返回。基于此,可以这样删除侦听器:
ipcRenderer.once(channel, listener);
为事件添加一个一次性侦听器函数。此侦听器仅在下次将消息发送到通道时调用,然后将其删除。
访问https://electron.atom.io/docs/api/ipc-renderer/ipcrendereroncechannel-listener
得到解决方案,并在这里工作。
相关文章:
- Ajax聊天消息重复而不仅仅是更新
- 如果localstorage为空,则显示欢迎消息
- 绑定时将Parsley minlength消息作为选项传递时,未对其进行自定义
- 离开页面时弹出消息
- 有没有一种方法可以在控制台关闭的情况下让console.log()在IE中记录消息
- 消息显示之外的淘汰验证
- 选项卡侦听器未被来自后台脚本的消息激活
- jQuery在输入下验证post错误消息
- Scipt未在选定选项卡中执行以发送和接收消息
- 检查xmlhttprequest问题的消息
- AngularJs对ng消息的自定义替换
- ng消息仅在触摸时显示错误,并在错误的初始显示上转换
- 当用户按下回车键时,自动在text区域/text中插入消息
- 如果消息框答案为YES,则用Javascript填写表单
- 在javaservlet doPost方法中启动线程时,无法返回异常消息
- 解析云代码错误:解析.错误{代码:141,消息:“未调用成功/错误”}
- Node.js上的WebSocket,并在所有连接的客户端之间共享消息
- 电子IPC消息过多
- IPC消息阻塞了电子中的进程
- JS IPC;从一个TAB发送消息到另一个TAB